みんなおはよう、
DataGridView を .txt ファイルに保存できるようにする C# コードのメソッドにいくつか問題があります。
コードは次のとおりです。
private void saveToTxt_Btn_Click(object sender, EventArgs e)
{
filenameText.Text = serviceDataGrid.Rows.Count.ToString();
//string toOutFile = @"C:\" + filenameText.Text+".txt";
string toOutFile = @"C:\hello.txt";
FileStream toFile = new FileStream(toOutFile, FileMode.Create);
TextWriter toText = new StreamWriter(toOutFile);
int count = serviceDataGrid.Rows.Count;
toText.WriteLine("\t\t" + filenameText.Text);
toText.WriteLine("\t\t" + directoryText.Text+"\n\n");
for (int row = 0; row < count-1; row++)
{
toText.WriteLine(serviceDataGrid.Rows[row].Cells[0].Value.ToString());
}
toText.Close();
toFile.Close();
}
次の行がエラーを返しています。
TextWriter toText = new StreamWriter(toOutFile);
IOException が処理されませんでした。 別のプロセスで使用されているため、プロセスはファイル 'C:\hello.txt' にアクセスできません。
問題が何であるかは完全にはわかりませんが、FileStream と TextWriter の間に競合があることを示唆しています。
誰でもこれに光を当てることができますか?よろしく