0

我创建了一个按钮。在按钮的事件处理程序中,我想删除文件夹(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”,因为它正被另一个进程使用。

4

1 回答 1

1

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

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

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

于 2013-10-06T04:05:37.087 回答