1

I use this following code to remove the write protection folder so that I can delete that. But It won't work.

File.SetAttributes(@"F:\File", FileAttributes.Normal); 
File.Delete(@"F:\File");

How can I remove the write protection?

If I can remove file protection from the disk, so give some codes to do that.

Any help will be appreciated

Thanks in advance

Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47

1 Answers1

1

There is a difference between Folder and File. With this you will remove the readonly atribute and delete the folder.

var di = new DirectoryInfo(@"F:\File");
di.Attributes &= ~FileAttributes.ReadOnly;
di.Delete(true);

EDIT:

Formating USB drive. You can read the article.

public static bool FormatDrive(string driveLetter, 
    string fileSystem = "NTFS", bool quickFormat=true, 
    int clusterSize = 8192, string label = "", bool enableCompression = false )
{
   if (driveLetter.Length != 2 || driveLetter[1] != ':'|| !char.IsLetter(driveLetter[0]))
      return false;

   //query and format given drive         
   ManagementObjectSearcher searcher = new ManagementObjectSearcher
    (@"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'");
   foreach (ManagementObject vi in searcher.Get())
   {
      vi.InvokeMethod("Format", new object[] 
    { fileSystem, quickFormat,clusterSize, label, enableCompression });
   }

   return true;
} 

You should put the driveLetter like this: "F:"

mybirthname
  • 17,949
  • 3
  • 31
  • 55