我正在使用.NET 3.5,尝试使用以下命令递归删除目录:
Directory.Delete(myPath, true);
Run Code Online (Sandbox Code Playgroud)
我的理解是,如果文件正在使用或存在权限问题,这应该抛出,否则它应该删除目录及其所有内容.
但是,我偶尔会得到这个:
System.IO.IOException: The directory is not empty.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
...
Run Code Online (Sandbox Code Playgroud)
我对这种方法有时会抛出并不感到惊讶,但是当递归为真时,我很惊讶地得到这条特殊的信息.(我知道目录不是空的.)
有没有理由我看到这个而不是AccessViolationException?
我正在尝试删除文件夹,并且由于包含长路径的文件夹,删除失败.我认为我需要使用其他东西而不是dir.Delete(真),任何人之前都穿过这座桥?
非常感谢
try
{
var dir = new DirectoryInfo(@FolderPath);
dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
dir.Delete(true);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
这是有问题的路径:\ server\share\dave\Private\Careers\Careers Ed\Fun Careers Education\Chris's not used used 2006 to07\old 4.Careers Area Activity Week 1 30.10.06 or 6.11.06 or 13.11.06职业水平和职业资源简介\职业领域和职位级别教师帮助表[1] .doc
我搜索了SO但却一无所获.
为什么这不起作用?
Directory.Delete(@"E:\3\{90120000-001A-0000-0000-0000000FF1CE}-C");
Run Code Online (Sandbox Code Playgroud)
上面的行将抛出异常"访问被拒绝".我有管理员权限,我可以用资源管理器删除目录.
它看起来像一些禁止的字符?但Windows资源管理器可以处理它.如何删除名称相似的目录?
我有一个带有实体框架的mvc4应用程序.
我想删除一个文件,但每次都说:
mscorlib.dll中出现"System.UnauthorizedAccessException"类型的异常,但未在用户代码中处理
附加信息:访问路径'G:\ Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ ContosoUniversity\Images \'被拒绝.
通过这一行:System.IO.File.Delete(path);
这是方法:
public ActionResult DeleteFiles(int id)
{
//var fileName = Path.GetFileName(id.FileName);
var DirSeparator = Path.DirectorySeparatorChar;
var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
var file = db.lolabikerPhotos.Find(id);
System.IO.File.Delete(path);
db.SaveChanges();
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
}
Run Code Online (Sandbox Code Playgroud)
我只是在visual studio中运行应用程序,如下所示: http://localhost:41787/Account/Edit?UserId=hallo
我已经完成了以下事情:
完全访问地图,我将网络服务添加到地图并完全控制.但似乎没有任何效果.我正在使用Windows 7.我以管理员身份运行visual studio 2013
我也看到了这个:
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has …Run Code Online (Sandbox Code Playgroud) c# windows entity-framework asp.net-mvc-4 visual-studio-2013
我正在使用System.IO.Directory.Delete并尝试删除系统文件夹,例如"我的音乐","我的视频"等,但是我收到的错误类似于"访问系统路径'C:\ users\jbloggs\Saved Games'被拒绝".然而,我可以通过资源管理器删除这些文件夹没有任何问题,我有这些文件夹的完全权限.关于我可以尝试的任何建议?
我的代码:
public static void ClearAttributes(string currentDir)
{
if (Directory.Exists(currentDir))
{
string[] subDirs = Directory.GetDirectories(currentDir);
foreach (string dir in subDirs)
ClearAttributes(dir);
string[] files = files = Directory.GetFiles(currentDir);
foreach (string file in files)
File.SetAttributes(file, FileAttributes.Normal);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
try
{
ClearAttributes(FolderPath);
System.IO.Directory.Delete("C:\\users\\jbloggs\\Saved Games", true);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud) 我的控制台应用程序正在创建一些运行时文件,所以我想要做的是在应用程序启动时删除所有这些文件.我试过这个:
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);
}
Run Code Online (Sandbox Code Playgroud)
...只是为了查找给定路径中的所有文件/文件夹(位于程序执行路径的子目录中),然后删除它们.但是,我得到以下异常:
访问路径"文件"被拒绝.
我试图以管理员身份运行该程序而没有运气; 但是,我想要一个不使用管理员权限的解决方案.
备注:
我想删除使用LibGit2Sharp从远程存储库克隆的本地repo文件夹.我在这里读到,我必须在删除它之前Dispose()存储库,但它仍然无法正常工作.
using (var repo = new LibGit2Sharp.Repository(path))
{
repo.Dispose();
}
Directory.DeleteFolder(path);
Run Code Online (Sandbox Code Playgroud)
我还有一个例外:
Access to the path 'c16566a7-202a-4c8a-84de-3e3caadd5af9' is denied.
Run Code Online (Sandbox Code Playgroud)
'path'变量的内容如下:
C:\Users\USERNAME\AppData\Local\dftmp\Resources\c16566a7-202a-4c8a-84de-3e3caadd5af9\directory\UserRepos\github.com\domonkosgabor\testrepo
Run Code Online (Sandbox Code Playgroud)
此文件夹由辅助角色创建到本地存储.
我该怎么做才能删除整个文件夹(包括.git)?
我目前使用此代码删除文件夹及其内容:
string tempFolder = System.Environment.GetEnvironmentVariable("HomeDrive");
System.IO.Directory.Delete(tempFolder + "\\" + "Test", true);
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但它会删除文件夹及其内容,但不会删除只读文件.那么如何使用c#有针对性的Framework 2.0来完成这个呢?
我创建了安装程序,需要删除一些文件和文件夹(如果它们已经存在)并替换为新的。
当用户单击安装时,安装程序会提示输入 UAC。选择“是”后,安装程序将调用删除现有文件和文件夹的方法。但它无法删除文件和文件夹。我使用的代码是 DI.Delete(path)。它给出了这个错误
Access to the path 'CodeCreate.aspx' is denied.
at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
at System.IO.DirectoryInfo.Delete(Boolean recursive)
at CustomizationCA.CustomActions.DeleteExistingFilesAndFolder(Session session)
Run Code Online (Sandbox Code Playgroud)
如果我以管理员身份运行命令提示符,然后使用“msiexec /i Setup.msi”命令,它会删除这些文件和文件夹。
当使用 Directory.Delete(path,true); 时 我将此视为例外
Exception thrown by custom action:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.
at System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean …Run Code Online (Sandbox Code Playgroud)