写保护模式不会改变

时间:2015-05-23 07:23:36

标签: c# registry file-attributes

我使用以下代码删除写保护文件夹,以便我可以删除它。但它不起作用。

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

如何删除写保护?

如果我可以从磁盘中删除文件保护,那么请提供一些代码来执行此操作。

任何帮助将不胜感激

提前致谢

1 个答案:

答案 0 :(得分:1)

文件夹和文件之间存在差异。这样,您将删除只读属性并删除该文件夹。

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

修改

Formating USB drive。你可以阅读这篇文章。

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;
} 

你应该像这样放置driveLetter:"F:"