FileStream.Write not Writing to File

孤街醉人 提交于 2021-02-16 21:33:47

问题


I'm passing some Base64 encoded strings through WCF, and I'm attempting to write them to a file. However, despite the fact that my FileStream object has a Length greater than 0, my file on disk remains empty.

FileStream fs = new FileStream(Config.Instance.SharedSettings.SaveDir + StudyInstance.StudyId + "\\tmp.ext", FileMode.Create);

EncodeBlock eb = new EncodeBlock();

while (eb.Part != eb.OfParts || eb.OfParts == 0)
{
    eb.ToDecode = ps.StudyService.GetInstancePart(StudyInstance, s, eb.Part+ 1, Config.Instance.ClientSettings.AppData);
    eb = Base64Encoder.Decode(eb);
    fs.Write(eb.ToEncode, 0, eb.ToEncode.Length);
}

fs.Close();

eb.ToEncode's length is always greater than 0, fs.Length is always greater than 0, and yet my "tmp.ext" file is created, but remains empty. fs.CanWrite is always also true.


回答1:


Have you tried calling FileStream.Flush?

Call it just before you Close the stream.

You should also use a "using" statement to ensure the stream is cleaned up.



来源:https://stackoverflow.com/questions/1704543/filestream-write-not-writing-to-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!