Access denied while deleting .exe file

Summary of the problem:

I have a console application which, after copying many folders and files to a new location, local drive, then deletes certain files / folders. One of these file types that it deletes is .exe files. When you try to delete the specified files, it gives me a negative access error (this also happens when you try to delete other types of files and folders)

Other notes:

I saw several questions such as Failed to delete .exe file through C #. However, this process never started on my local machine or in the source that was copied from. I am both a local administrator and a domain administrator in our domain, and I had the right to own all the folders and files in the directories I work with. But when I try to delete the file from the code, it still refuses me. However, I can delete such files / folders manually (click the "Delete" button)

Problem:

As stated above, I’m looking for a way to overcome this problem by denying me access to delete these Illegal file types and remove them from my code.

My code: (Updated)

#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 

Any help would be greatly appreciated. If this question was answered directly elsewhere, feel free to mention it in a comment. As I said above, I looked through the last question on this issue and have not yet found a solution. Otherwise, thank you for your help.

+5
source share
1 answer

It turned out that the problem is that the files are marked as "Read Only", so I added an if statement before deleting the file to check if this was the case, and remove the label if necessary. Here is the code that worked successfully to delete all the necessary files.

 #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 

Thanks to those who commented and helped. And hope this can help others in the future.

+1
source

Source: https://habr.com/ru/post/1209720/


All Articles