问题摘要:
我有一个控制台应用程序,在将许多文件夹和文件复制到新位置(本地驱动器)之后,它会删除某些文件/文件夹。它删除的这些文件类型之一是.exe文件。当试图删除所述文件时,它会给我一个拒绝访问错误。(这也会在尝试删除其他类型的文件和文件夹时发生)
其他说明:
我看到了几个问题,例如Unable to delete .exe file through c#.但是,该过程从未在我的本地计算机上运行,也没有在从它复制的源上运行。我既是本地管理员又是域名管理员,我对我正在使用的目录中的所有文件夹和文件拥有所有权。但是在尝试从代码中删除文件时,它仍然拒绝我访问。但我可以手动删除这些文件/文件夹。(点击删除键)
问题:
如上所述,我正在寻找一种方法来解决此问题,拒绝我删除这些“非法”文件类型并将其从我的代码中删除。
我的代码:(已更新)
#region Delete_Illegal_Items
public static void RemoveIllegalItems()
{
Console.Clear();
DirectoryInfo Libraries = new DirectoryInfo(Library.DestinationMain);
try
{
foreach (var Lib in Libraries.GetDirectories())
{
Console.WriteLine("Working On {0}.", Lib.Name);
Parallel.Invoke(
() =>
{
RemoveBadFiles(Lib);
},
() =>
{
DeleteEmptyFolders(Lib);
}
);
}
}
catch (AggregateException e)
{
Console.WriteLine("There Was An Unusual Error During Initialization Of Library Correction:\n{0}", e.InnerException.ToString());
}
}
private static string[] BadFiles = {
@".hta",
@".exe",
@".lnk",
@".tmp",
@".config",
@".ashx",
@".hta.",
@".hta::$DATA",
@".zip",
@".asmx",
@".json",
@".soap",
@".svc",
@".xamlx",
@".msi",
@".ops",
@".pif",
@".shtm",
@".shtml",
@"smt",
@".vb",
@".vbe",
@".vbs",
@".ds_store",
@".db",
@".ini",
@".tiff"
};
private static void RemoveBadFiles(DirectoryInfo directory)
{
DirectoryInfo[] dirs = null;
FileInfo[] files = null;
if (directory != null)
{
files = directory.GetFiles();
}
try
{
dirs = directory.GetDirectories();
}
catch (IOException) { }
catch (Exception e)
{
Console.WriteLine("\nError During Enumeration Of Items To Delete:\n{0}", e.Message);
}
if (files != null)
{
foreach (var file in files)
{
try
{
if (file.IsReadOnly)
{
file.IsReadOnly = false;
}
if (BadFiles.Contains(Path.GetExtension(file.FullName)))
{
File.Delete(file.FullName);
}
}
catch (Exception e)
{
Console.WriteLine("\nError During Removal Or Illegal Files:\n" + e.Message);
}
}
}
if (dirs != null)
{
foreach (var dir in dirs)
{
switch (dir.Name)
{
case ".TemporaryItems":
{
try
{
Directory.Delete(dir.FullName);
}
catch { }
break;
}
case "AI_RecycleBin":
{
try
{
Directory.Delete(dir.FullName);
}
catch { }
break;
}
case ".ToRemove":
{
try
{
Directory.Delete(dir.FullName);
}
catch { }
break;
}
default:
{
break;
}
}
RemoveBadFiles(dir);
}
}
}
private static void DeleteEmptyFolders(DirectoryInfo directory)
{
Program Main = new Program();
try
{
DirectoryInfo[] dirs = directory.GetDirectories();
foreach (var subDirectory in dirs)
{
int sum = Library.CountLibrary(subDirectory.FullName);
if (sum == 0)
{
Directory.Delete(subDirectory.FullName);
}
DeleteEmptyFolders(subDirectory);
}
}
catch { }
}
#endregion
非常感谢任何帮助。如果这个问题已在其他地方直接回答,请随时在评论中提及。如上所述,我一直在查看有关此问题的过去问题,但尚未找到解决方案。不然谢谢你的帮助。
答案 0 :(得分:1)
弄清楚问题是文件被标记为“只读”所以我在删除文件之前添加了一个if语句来检查它是否是,如果需要则删除标记。这是成功删除所有想要的文件的代码。
#region Delete_Illegal_Items
public static void RemoveIllegalItems()
{
Console.Clear();
DirectoryInfo Libraries = new DirectoryInfo(Library.DestinationMain);
try
{
foreach (var Lib in Libraries.GetDirectories())
{
Console.WriteLine("Working On {0}.", Lib.Name);
Parallel.Invoke(
() =>
{
RemoveBadFiles(Lib);
},
() =>
{
DeleteEmptyFolders(Lib);
}
);
}
}
catch (AggregateException e)
{
Console.WriteLine("There Was An Unusual Error During Initialization Of Library Correction:\n{0}", e.InnerException.ToString());
}
}
private static string[] BadFiles = {
@".hta",
@".exe",
@".lnk",
@".tmp",
@".config",
@".ashx",
@".hta.",
@".hta::$DATA",
@".zip",
@".asmx",
@".json",
@".soap",
@".svc",
@".xamlx",
@".msi",
@".ops",
@".pif",
@".shtm",
@".shtml",
@"smt",
@".vb",
@".vbe",
@".vbs",
@".ds_store",
@"ds_store",
@"._.Trashes",
@".Trashes",
@".db",
@".dat",
@".sxw",
@".ini",
@".tif",
@".tiff"
};
private static void RemoveBadFiles(DirectoryInfo directory)
{
DirectoryInfo[] dirs = null;
FileInfo[] files = null;
if (directory != null)
{
try
{
files = directory.GetFiles();
}
catch (IOException) { }
}
try
{
dirs = directory.GetDirectories();
}
catch (IOException) { }
catch (Exception e)
{
Console.WriteLine("\nError During Enumeration Of Items To Delete:\n{0}", e.Message);
}
if (files != null)
{
foreach (var file in files)
{
try
{
if (file.IsReadOnly)
{
file.IsReadOnly = false;
}
if (BadFiles.Contains(Path.GetExtension(file.FullName)) || BadFiles.Contains(file.Name))
{
File.Delete(file.FullName);
}
}
catch (Exception e)
{
Console.WriteLine("\nError During Removal Or Illegal Files:\n" + e.Message);
}
}
}
if (dirs != null)
{
foreach (var dir in dirs)
{
switch (dir.Name)
{
case ".TemporaryItems":
{
try
{
Directory.Delete(dir.FullName);
}
catch { }
break;
}
case "TemporaryItems":
{
try
{
Directory.Delete(dir.FullName);
}
catch { }
break;
}
case "AI_RecycleBin":
{
try
{
Directory.Delete(dir.FullName);
}
catch { }
break;
}
case ".ToRemove":
{
try
{
Directory.Delete(dir.FullName);
}
catch { }
break;
}
default:
{
break;
}
}
RemoveBadFiles(dir);
}
}
}
private static void DeleteEmptyFolders(DirectoryInfo directory)
{
Program Main = new Program();
try
{
DirectoryInfo[] dirs = directory.GetDirectories();
foreach (var subDirectory in dirs)
{
int sum = Library.CountLibrary(subDirectory.FullName);
if (sum == 0)
{
Directory.Delete(subDirectory.FullName);
}
DeleteEmptyFolders(subDirectory);
}
}
catch { }
}
#endregion
感谢那些评论和帮助的人。希望这可以在将来帮助其他人。