C# 是否有使用 StreamWriter 可以写入的最大字符数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/163550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:20:35  来源:igfitidea点击:

Is there a maximum number of characters that can be written using a StreamWriter?

c#.netfile-io

提问by Josh Mein

Is there a maximum number of characters that can be written to a file using a StreamWriter? Or is there a maximum number of characters that WriteLine()can output? I am trying to write some data to a file but all of the data does not seem to make it. This is the current state of my code:

是否有使用 StreamWriter 可以写入文件的最大字符数?或者有WriteLine()可以输出的最大字符数吗?我正在尝试将一些数据写入文件,但所有数据似乎都没有写入。这是我的代码的当前状态:

StreamWriter sw = new StreamWriter(pathToFile);

foreach (GridViewRow record in gv_Records.Rows)
{
    string recordInfo = "recordInformation";

    sw.WriteLine(recordInfo);
}

采纳答案by itsmatt

Are you calling StreamWriter.Close() or Flush()?

你是在调用 StreamWriter.Close() 还是 Flush()?

回答by user7116

Be sure you wrap your StreamWriter in a using-block, or are careful about your explicit management of the resource's lifetime.

确保将 StreamWriter 包装在 using 块中,或者小心您对资源生命周期的显式管理

using (StreamWriter writer = new StreamWriter(@"somefile.txt"))
{
    // ...
    writer.WriteLine(largeAmountsOfData);
    // ...
}

回答by MagicKat

Make sure that you are calling .Flush()

确保您正在调用 .Flush()