C# File.Delete 对路径的访问被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15260146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:43:41  来源:igfitidea点击:

File.Delete Access to the path is denied

c#file

提问by Roman Ratskey

My console application program is creating some runtime files while it is working so what I want to do is delete all of these files on the application startup. I have tried this:

我的控制台应用程序在运行时正在创建一些运行时文件,所以我想要做的是在应用程序启动时删除所有这些文件。我试过这个:

public static void Empty(string targetDir)
{
    var directory = new DirectoryInfo(targetDir);
    if (!directory.Exists) return;
    foreach (var file in directory.GetFiles()) file.Delete();
    foreach (var subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
}

...just to look for all the files/folders in the given path (which is in a subdirectory in the program execution path) then delete them. However, I get the following exception:

...只是为了查找给定路径(位于程序执行路径的子目录中)中的所有文件/文件夹,然后删除它们。但是,我收到以下异常:

Access to the path 'file' is denied.

拒绝访问路径“文件”。

I tried to run the program as administrator with no luck; However, I want a solution that works without using administrator privileges.

我试图以管理员身份运行该程序,但没有运气;但是,我想要一个无需使用管理员权限即可工作的解决方案。

Notes :

注意事项:

  1. The file is not running in another application.
  2. The file is not in a protected folder.
  3. The file can be deleted manually with no problems and that's why i am here.
  1. 该文件未在其他应用程序中运行。
  2. 该文件不在受保护的文件夹中。
  3. 该文件可以毫无问题地手动删除,这就是我在这里的原因。

采纳答案by sa_ddam213

Try using the Microsoft.VisualBasic.FileIO.FileSystemmethods as it has a handy DeleteDirectorymethod, I had access troubles awhile ago and this was the fix for my problem.

尝试使用这些Microsoft.VisualBasic.FileIO.FileSystem方法,因为它有一个方便的DeleteDirectory方法,前一段时间我遇到了访问问题,这是我的问题的解决方案。

var directory = new DirectoryInfo(targetDir);
if (directory.Exists)
{
    Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(targetDir, Microsoft.VisualBasic.FileIO.DeleteDirectoryOption.DeleteAllContents);
}

回答by Roman Ratskey

Using Windows API MoveFileEx might be a potential solution with a parameter MOVEFILE_DELAY_UNTIL_REBOOT to remove the file only after reboot.

使用 Windows API MoveFileEx 可能是一个潜在的解决方案,它带有参数 MOVEFILE_DELAY_UNTIL_REBOOT 以仅在重新启动后删除文件。

Please check http://msdn.microsoft.com/en-us/library/aa365240%28v=vs.85%29.aspx.

请检查http://msdn.microsoft.com/en-us/library/aa365240%28v=vs.85%29.aspx

回答by EdmundYeung99

You say that the files are not open in another application, but it must be open within your application:

你说文件没有在另一个应用程序中打开,但它必须在你的应用程序中打开:

//Create some directories to delete
Directory.CreateDirectory("C:/Temp/DeleteMe");
Directory.CreateDirectory("C:/Temp/DeleteMe/DeleteMe");
File.Create("C:/Temp/DeleteMe/DeleteMeFile");//FileStream still open!!

//Delete the files
var directory = new DirectoryInfo("C:/Temp/DeleteMe");
if (!directory.Exists) return;
foreach (FileInfo file in directory.GetFiles())
{
    file.Delete();
}
foreach (DirectoryInfo dir in directory.GetDirectories())
{
    dir.Delete(true);
}

Make sure you dispose the file stream when you create the file

确保在创建文件时处理文件流

//Create some directories to delete
Directory.CreateDirectory("C:/Temp/DeleteMe");
Directory.CreateDirectory("C:/Temp/DeleteMe/DeleteMe");
using (File.Create("C:/Temp/DeleteMe/DeleteMeFile")) { }

//Delete the files
var directory = new DirectoryInfo("C:/Temp/DeleteMe");
if (!directory.Exists) return;
foreach (FileInfo file in directory.GetFiles())
{
    file.Delete();
}
foreach (DirectoryInfo dir in directory.GetDirectories())
{
    dir.Delete(true);
}

回答by majjam

I got this error and found that it was because my test files were readonly. Changed this and I can now use fileinfo to delete them no worries.

我收到这个错误,发现是因为我的测试文件是只读的。改变了这一点,我现在可以使用 fileinfo 来删除它们,不用担心。

回答by roderickprince

if (File.Exists(filePath))
{
    File.SetAttributes(filePath, FileAttributes.Normal);
    File.Delete(filePath);
}