4

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

user990635
  • 3,979
  • 13
  • 45
  • 66
  • 2
    What are you trying to do with the stream afterwards? (Most streams don't have a "name" etc.) `MemoryStream` *may* be good enough for you, but we don't know what you're trying to achieve. – Jon Skeet Nov 27 '13 at 12:06
  • 1
    What do you mean? new FileStream doesn't demand that the file actually exists yet, so you can use it just fine to create a file. Simply use something like `FileMode.Create`. – Tobberoth Nov 27 '13 at 12:07
  • Why don't you use MemoryStream when you don't need file, later when you want to save it to File you can convert it. http://stackoverflow.com/questions/18766055/copy-memorystream-to-filestream-and-save-the-file – Imran Rizvi Nov 27 '13 at 12:09
  • 1
    A [`FileStream`](http://msdn.microsoft.com/en-us/library/system.io.filestream%28v=vs.110%29.aspx) *is* a [`Stream`](http://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx). No need to convert anything there. Also, when you have a `FileStream`, you automatically have a file, as the `FileStream` is like an object-oriented representation for the file. There is no such thing as a `FileStream` without a file, though there are different stream types that behave basically the same as a `FileStream` that are not backed by a file. – O. R. Mapper Nov 27 '13 at 12:09
  • Some clue as to why you think you need to create this hoop, so you can then trip over it would be good. – Tony Hopkinson Nov 27 '13 at 12:14
  • @JonSkeet I will add an edit to the question – user990635 Nov 27 '13 at 12:16
  • I have written an answer based on the information from the original version of your question, but now that I'm seeing your source code: Where in there do you actually need the `Name` property of the stream? – O. R. Mapper Nov 27 '13 at 12:22
  • 1
    Your question is a bit confusing - if you want an empty file (i.e. an actual file with no content - size=0), why don't you just create a file with no content? See [this question](http://stackoverflow.com/questions/802541/creating-an-empty-file-in-c-sharp). You can then write to it later. If you need a file that's invisible in the system, why would you need a `FileStream` in the first place? – w128 Nov 27 '13 at 12:32

1 Answers1

1

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:

  • You could store all the properties you need in the wrapper.
  • The wrapper could wrap an arbitrary 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.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114