小编Tan*_*eng的帖子

为什么在StreamReader.Read周围使用using(){}允许之后删除文件?

所以我正在为学校项目尝试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)

c# using streamreader winforms system.io.file

1
推荐指数
1
解决办法
64
查看次数

标签 统计

c# ×1

streamreader ×1

system.io.file ×1

using ×1

winforms ×1