所以这是我当前的代码,我使用该文件写入,创建,然后再次写入,但它说它被另一个进程使用。
private void btn_Finish_Click(object sender, EventArgs e)
{
if((txt_setText.Text != "" || txt_setText.Text != "Enter your text here...") && txt_copyItem.Text != "")
{
lbl_error.Visible = false;
//gets the amount of enties made and sets the int
amount = Convert.ToInt32(File.ReadAllText(@"Files/data.txt"));
amount += 1;
File.WriteAllText(@"Files/data.txt", amount.ToString());
//execute addCopy function
addCopy(txt_copyItem.Text, txt_setText.Text);
MainTab.SelectedIndex = 0;
}
else
{
lbl_error.Visible = true;
}
}
private void addCopy(string name, string clip)
{
string path = @"Files/User/c" + amount.ToString() + ".txt";
File.Create(path);
File.WriteAllText(path, (name + "," + clip));
}
当我操作代码时,我收到此错误:
该进程无法访问文件' C:\ Users \ jakey \ onedrive \ documents \ visual studio 2015 \ Projects \ copyboard \ copyboard \ bin \ Debug \ Files \ User \ c1.txt'因为它正被另一个进程使用。
我该如何解决? 提前致谢:D
答案 0 :(得分:-2)
解决:
private void addCopy(string name, string clip)
{
string path = @"Files/User/c" + amount.ToString() + ".txt";
StreamWriter w = new StreamWriter(path);
w.WriteLine((name + "," + clip));
w.Close();
}
刚刚用StreamWriter替换了File.Create(path)