2

I have a stream saved to a file the following way:

public void SaveTest(DataObject data) { var fullPath = Path.Combine(Path.GetTempPath(), data.Descriptor.Name); var fileStream = new FileStream(Path.Combine(fullPath, ".content"), FileMode.CreateNew); data.Content.CopyTo(fileStream); fileStream.Close(); var information = new XElement("Test Information", new XAttribute("Name", data.Descriptor.Name), new XAttribute("Description", data.Descriptor.Description), new XAttribute("Owner", data.Descriptor.Owner) ); information.Save(Path.Combine(fullPath, ".information")); } 

DataObject contains a stream which is the test's content and a descriptor with 3 fields I'd like to save.
Now I need a method to read those files, and I need to read the .content file as a stream, not string. How can I do that?

1 Answer 1

3

Well, you can open it as a stream as simply as:

using (var stream = File.OpenRead(path)) { ... } 

Or you can read the whole lot as a byte array:

byte[] data = File.ReadAllBytes(path); 

(You can then wrap that in a MemoryStream if you want.)

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

2 Comments

Hi, I have a related question if you don't mind answering - After saving the stream using a file stream, when I read it and compare the two streams (using Assert), they are not equal. Do you know why?
@Noich: Stream doesn't override Equals. If you want to compare the contents, you'll need to read the data and compare that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.