从文件夹,其子文件夹及其中的所有文件中删除只读

有没有办法,我可以删除属性“只读”?

我试过这个:

var di = new DirectoryInfo("C:\\Work"); di.Attributes &= ~FileAttributes.ReadOnly; 

但它不能完成这项工作

差不多,试试:

 var di = new DirectoryInfo("C:\\Work"); foreach (var file in di.GetFiles("*", SearchOption.AllDirectories)) file.Attributes &= ~FileAttributes.ReadOnly; 

做得更好的方法可能是。

 string cmd = string.Format(" /C ATTRIB -R \"{0}\\*.*\" /S /D", binPath); CallCommandlineApp("cmd.exe", cmd); private static bool CallCommandlineApp(string progPath, string arguments) { var info = new ProcessStartInfo() { UseShellExecute = false, RedirectStandardOutput = true, FileName = progPath, Arguments = arguments }; var proc = new Process() { StartInfo = info }; proc.Start(); using (StreamReader stReader = proc.StandardOutput) { string output = stReader.ReadToEnd(); Console.WriteLine(output); } // TODO: Do something with standard error. proc.WaitForExit(); return (proc.ExitCode == 0) ? true : false; } 

我遇到了这个陌生的问题,想要清除目录的读/写标志以及它可能具有的任何子文件/目录。 并且使用foreach循环声音就像额外的工作一样,当Attrib命令行function正常工作并且几乎是瞬时的。

这是我在谷歌搜索中发现的。

 File.SetAttributes(filePath, File.GetAttributes(filePath) & ~(FileAttributes.ReadOnly)); 

显然这仅适用于一个文件,因此您必须循环遍历文件并为每个文件设置属性。