c# - 如何结束进程?

标签 c# .net file-io

这部分

if (File.Exists(filePath)) {
      string status = new StreamReader(File.OpenRead(filePath)).ReadLine();

      if (status != "SUCCEEDED") {
           File.Delete(filePath);
           createDb();
      } 
}

程序给出异常消息

The process can not access the file '\STATUS.txt' because it is being used by another process.

如何解决这个问题?

最佳答案

修改你的代码如下:

if (File.Exists(filePath)) 
{
    string status;
    using(var streamReader = new StreamReader(filePath))
    {
        status = streamReader.ReadLine();
    }

    if (status != "SUCCEEDED")
    {
        File.Delete(filePath);
        createDb();
    }
}

关于c# - 如何结束进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24141884/

相关文章:

c# - 以编程方式设置唤醒时间

c# - 如何在C#中指定相对SQLite数据库路径?

c# - WPF `Glyphs` 没有像 `TextBlock` 那样正确渲染文本

c# - 如何使机器独立和 .Net 框架(依赖项)独立于 .Net 的 'EXE' 文件

java - 在目录中搜索 .txt 文件,无需完整路径名。 - java

c# - 从 Autofac 函数工厂获取每个请求的依赖性

c# - 如何为 SignalR 生成 API 文档

c# - 如何为电子邮件创建标识图标?

c++ - sprintf 生成的字符串的值顺序与 Ubuntu 服务器上传递的参数顺序不同

java - 在 java 中读取大型 CSV