1

list<string>約350行(13列)の.csvファイルをC#で書き込もうとしています。ループでファイルに書き込みますが、リストの一部 (206 行半) しかファイルに書き込まれません。これは私のコードです:

        StreamWriter file = new StreamWriter(@"C:\test.csv", true);
        foreach (string s in MyListString)
        {
            Console.WriteLine(s); // Display all the data
            file.WriteLine(s);    // Write only a part of it
        }

ファイルが正しく入力されないのはなぜですか? 考慮すべき制限はありますか?

4

3 に答える 3

5

FlushまたはCloseライターが必要なようです。usingまた、ほとんどの場合、ライターをステートメントでラップしたいと思うでしょう。

幸いなことに、破棄するとライターが自動的に閉じられ、書き込むアイテムの最後のバッチがフラッシュされるため、問題が解決するだけでなく、管理されていないアイテムを破棄することもできます。

次のことを試してください。

using (StreamWriter file = new StreamWriter(@"C:\test.csv", true))
{
    foreach (string s in MyListString)
    {
        Console.WriteLine(s); // Display all the data
        file.WriteLine(s);    // Write only a part of it
    }
}
于 2013-07-08T09:42:42.617 に答える
2

ストリームを閉じる必要があります:

using(StreamWriter file = new StreamWriter(@"C:\test.csv", true))
{
    foreach (string s in MyListString)
    {
        Console.WriteLine(s); // Display all the data
        file.WriteLine(s);    // Write only a part of it
    }
}
于 2013-07-08T09:42:38.927 に答える
0
using (StreamWriter file = new StreamWriter(@"C:\test.csv", true)){
    foreach (string s in MyListString)
    {
        Console.WriteLine(s); // Display all the data
        file.WriteLine(s);    // Write only a part of it

    }
}
于 2013-07-08T09:42:45.973 に答える