Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a directory with read-only files in C#?

I need to delete a directory that contains read-only files. Which approach is better:

  • Using DirectoryInfo.Delete(), or,

  • ManagementObject.InvokeMethod("Delete")?

With DirectoryInfo.Delete(), I have to manually turn off the read-only attribute for each file, but ManagementObject.InvokeMethod("Delete") doesn't appear to need to. Is there any situation where one is more preferable to the other?

Sample code (test.txt is read only).

First way:

DirectoryInfo dir = new DirectoryInfo(@"C:\Users\David\Desktop\"); dir.CreateSubdirectory("Test");  DirectoryInfo test = new DirectoryInfo(@"C:\Users\David\Desktop\Test\"); File.Copy(@"C:\Users\David\Desktop\test.txt", @"C:\Users\David\Desktop\Test\test.txt"); File.SetAttributes(@"C:\Users\David\Desktop\Test\test.txt", FileAttributes.Archive); test.Delete(true); 

Second way:

DirectoryInfo dir = new DirectoryInfo(@"C:\Users\David\Desktop\"); dir.CreateSubdirectory("Test");  DirectoryInfo test = new DirectoryInfo(@"C:\Users\David\Desktop\Test\"); File.Copy(@"C:\Users\David\Desktop\test.txt", @"C:\Users\David\Desktop\Test\test.txt");  string folder = @"C:\Users\David\Desktop\Test"; string dirObject = "Win32_Directory.Name='" + folder + "'"; using (ManagementObject managementObject = new ManagementObject(dirObject)) {     managementObject.Get();     ManagementBaseObject outParams = managementObject.InvokeMethod("Delete", null,     null);     // ReturnValue should be 0, else failure     if (Convert.ToInt32(outParams.Properties["ReturnValue"].Value) != 0)     {     } } 
like image 249
David Hodgson Avatar asked Mar 04 '09 18:03

David Hodgson


People also ask

How do you delete read only files?

Remove the read-only attribute Some read-only files can be changed to allow for edits by removing the read-only attribute in the file properties. Right-click the file and select Properties. Uncheck the box for Read-only and click OK.

Can a read only document be deleted?

"Read Only" files can changed and deleted by other users on your network if you give them the access to do so. If you are still unable to delete a Read Only file on a Windows computer, try a utility program such as MoveOnBoot, Delete FXP Files, Delinvfile or Unlocker.

How do you destroy a directory?

To delete an empty directory, use the -d ( --dir ) option and to delete a non-empty directory, and all of its contents use the -r ( --recursive or -R ) option.

How do you remove a directory and all files in it?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.


2 Answers

Simplest way of avoiding recursive calls is by utilising the AllDirectories option when getting FileSystemInfos, like so:

public static void ForceDeleteDirectory(string path)  {     var directory = new DirectoryInfo(path) { Attributes = FileAttributes.Normal };      foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))     {         info.Attributes = FileAttributes.Normal;     }      directory.Delete(true); } 
like image 160
adrian Avatar answered Sep 29 '22 03:09

adrian


Here is an extension method which sets Attributes to Normal recursively, then deletes the items:

public static void DeleteReadOnly(this FileSystemInfo fileSystemInfo) {     var directoryInfo = fileSystemInfo as DirectoryInfo;         if (directoryInfo != null)     {         foreach (FileSystemInfo childInfo in directoryInfo.GetFileSystemInfos())         {             childInfo.DeleteReadOnly();         }     }      fileSystemInfo.Attributes = FileAttributes.Normal;     fileSystemInfo.Delete(); } 
like image 29
Vitaliy Ulantikov Avatar answered Sep 29 '22 05:09

Vitaliy Ulantikov