Checking if a stream is empty

Erik picture Erik · May 30, 2011 · Viewed 31.6k times · Source

I am trying to deserialize a XML-file. I need to check if the XML-file stream is empty before tying to deserialize it.

IsolatedStorageFileStream isfs1 = new IsolatedStorageFileStream("test.xml", 
    FileMode.Open, FileAccess.Read, isf);

// Deserialize the XML to an object
Settings s = new Settings();
SoapFormatter SF= new SoapFormatter();
s = (Settings) SF.Deserialize(isfs1); 

How can I check if isfs1 empty or not?

Answer

Oded picture Oded · May 30, 2011

Check the Length property of the stream.

Length represents the number of bytes currently in the file.

If it is 0, the file is empty.