问题
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