【问题标题】:How do I close another process to release file locks? [duplicate]如何关闭另一个进程以释放文件锁? [复制]
【发布时间】:2013-10-06 04:00:44
【问题描述】:

我创建了一个按钮。在按钮的事件处理程序中,我想删除文件夹(abc)中的所有文件。

下面是代码:

    private void button1_Click_1(object sender, EventArgs e)
    {
        MessageBox.Show("Are you sure!!!! The files in the folder will be deleted permanently");
        this.Close();
        string[] filePaths = Directory.GetFiles(@"C:\abc\");
        foreach (string filePath in filePaths)
            File.Delete(filePath);
    }

例如,文件夹中有一个 Word 文件,如果打开它,我会收到一条错误消息:

进程无法访问文件'C:\abc\New Microsoft Word Document.docx',因为它正被另一个进程使用。

【问题讨论】:

标签: c# .net windows file io


【解决方案1】:

您可以使用Process 类查找该进程,强制关闭该程序,然后删除该文件。像这样的...

Process [] proc Process.GetProcessesByName("winword");
proc[0].Kill();

但是我不建议这样做,因为 windows 也不会删除打开的文件。

【讨论】:

  • 还有什么建议吗????
  • 谢谢@Nikhil.......它的工作......
猜你喜欢
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
相关资源
最近更新 更多