Reputation: 4249
Is it possible to create a filestream without an actual file?
I'll try to explain:
I know how to create a stream from a real file:
FileStream s = new FileStream("FilePath", FileMode.Open, FileAccess.Read);
But can I create a fileStream with a fake file?
meaning:
define properties such as name, type, size, whatever else is necessary, to some file object (is there such thing?), without a content, just all the properties,
and after that to create a fileStream from this "file"? to have the result similar to the above code?
edit.
I am using an API sample that has that code:
FileStream s = new FileStream("FilePath", FileMode.Open, FileAccess.Read);
try
{
SolFS.SolFSStream stream = new SolFS.SolFSStream(Storage, FullName, true, false, true, true, true, "pswd", SolFS.SolFSEncryption.ecAES256_SHA256, 0);
try
{
byte[] buffer = new byte[1024*1024];
long ToRead = 0;
while (s.Position < s.Length)
{
if (s.Length - s.Position < 1024*1024)
ToRead = s.Length - s.Position;
else
ToRead = 1024 * 1024;
s.Read(buffer, 0, (int) ToRead);
stream.Write(buffer, 0, (int) ToRead);
}
So it is basically writes fileStream "s" somewhere.
I don't want to take an existing file and write it, but I want to "create" a different file without the content (I don't need the content) but to have the properties of the real file such as size, name, type
Upvotes: 4
Views: 7908
Reputation: 20731
Apparently, you want to have a FileStream
(explicitly with its FileStream
-specific properties such as Name
) that does not point to a file.
This is, to my knowledge, not possible based on the implementation of FileStream
.
However, creating a wrapper class with the required properties would be a straightforward solution:
Stream
, so you would be free to choose between FileStream
, MemoryStream
, or any other stream type.Here is an example:
public class StreamContainer
{
public StreamContainer(string name, Stream contents)
{
if (name == null) {
throw new ArgumentNullException("name");
}
if (contents == null) {
throw new ArgumentNullException("contents");
}
this.name = name;
this.contents = contents;
}
private readonly string name;
public string Name {
get {
return name;
}
}
private readonly Stream contents;
public Stream Contents {
get {
return contents;
}
}
}
Of course, you could then add some courtesy creation methods for various stream types (as static methods in the above class):
public static StreamContainer CreateForFile(string path)
{
return new StreamContainer(path, new FileStream(path, FileMode.Open, FileAccess.Read));
}
public static StreamContainer CreateWithoutFile(string name)
{
return new StreamContainer(name, new MemoryStream());
}
In your application, whereever you want to use such a named stream, pass around the StreamContainer
rather than expecting a Stream
directly.
Upvotes: 1