需要你的眼睛。
void testSave()
{
fileName = curMonth.Text + curYear.Text + ".csv";
if (!File.Exists(path+fileName))
{
File.Create(path + fileName);
}
else if (File.Exists(path + fileName))
{
try
{
lines = curDate.Text + "," + myDesc.Text + "," + myAmount.Text;
File.AppendAllText(path + fileName, lines + Environment.NewLine);
TextWriter tw = new StreamWriter(path + fileName, true);
tw.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
错误如下:
该进程无法访问文件'C:\ Users ... \ File.csv',因为它正由另一个进程使用。
我可以参考的任何建议或解决方案?谢谢
答案 0 :(得分:0)
File.Create(path + fileName);
未关闭,因此您无法重新开启。
如果需要,请删除else
关键字。
void testSave()
{
fileName = curMonth.Text + curYear.Text + ".csv";
if (!File.Exists(path+fileName))
{
try
{
File.Create(path + fileName).Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
try
{
lines = curDate.Text + "," + myDesc.Text + "," + myAmount.Text;
File.AppendAllText(path + fileName, lines + Environment.NewLine);
TextWriter tw = new StreamWriter(path + fileName, true);
tw.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}