C# – Checking if a stream is empty

.netc++deserializationstream

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?

Best Solution

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.