所以我正在为学校项目尝试Windows表单并继续遇到错误:
System.IO.IOException('进程无法访问文件'C:\ XXXX\YYYY.txt',因为它正由另一个进程使用.'
当试图File.Delete(path);通过button_click事件删除文件()时.
事实证明,当我改变以下方法时:
private void updateTxt(){
String tempStore = "";
iDLbl1.Text = "ID:" + id;//iDLbl1 is ID Label 1
try
{
StreamReader Reader = new StreamReader(path);
while (!Reader.EndOfStream)
{ tempStore += Reader.ReadLine() + "\n"; }
}
catch { noIDLbl.Visible = true; }
rTxtBox.Text = tempStore;//rTxtBox is Rich Text Box
}
Run Code Online (Sandbox Code Playgroud)
至
private void updateTxt(){
String tempStore = "";
iDLbl1.Text = "ID:" + id;//iDLbl1 is ID Label 1
try
{
using(StreamReader Reader = new StreamReader(path)) …Run Code Online (Sandbox Code Playgroud)