检查流是否为空

Eri*_*rik 18 .net c# stream deserialization

我试图反序列化XML文件.在绑定反序列化之前,我需要检查XML文件流是否为空.

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); 
Run Code Online (Sandbox Code Playgroud)

我怎样检查是否isfs1空?

Ode*_*ded 30

检查Length流的属性.

Length表示文件中当前的字节数.

如果为0,则文件为空.


小智 5

如果您的文件是 UTF-8 格式,则由于 BOM(字节顺序标记),它的大小至少为 3。因此,即使您的文件为空,检查长度 > 0 也会返回 true。

  • 所以?解决办法是什么? (2认同)