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

8
  • 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. Commented Nov 27, 2013 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. Commented Nov 27, 2013 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. stackoverflow.com/questions/18766055/… Commented Nov 27, 2013 at 12:09
  • 1
    A FileStream is a Stream. 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. Commented Nov 27, 2013 at 12:09
  • 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. 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? Commented Nov 27, 2013 at 12:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I used a Dictionary<string,Stream>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.