-3

I have a StreamWriter that seems to take a long time opening and closing a file. I can visibly see in windows explorer the file size switching between 0KB and 1KB (StreamWriter is in a timer). It is 0KB for about half the time, when it should be 0KB (open) for such a small amount of time that I shouldn't be able to see it. Any solutions to fix this?

4

1 回答 1

1

也许你StreamWriter在不需要的时候打开,所以using只在你处理文件时尝试使用。这也将提高可读性。

例如:

using (Stream st = File.Open("file.ext", FileMode.OpenOrCreate, FileAccess.ReadOrWrite))
        {
            using (StreamWriter wr=new StreamWriter(st))
            {
                //your code that needs StreamWriter
            }
        }
于 2013-05-11T17:28:33.767 回答