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 { } }
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.