Remove readonly attribute from directory

前端 未结 8 950
情歌与酒
情歌与酒 2020-12-01 06:23

How can I programatically remove the readonly attribute from a directory in C#?

相关标签:
8条回答
  • 2020-12-01 06:36

    Here's a good link to examples of modifying file attributes using c#

    http://www.csharp-examples.net/file-attributes/

    based on their example, you can remove the Read Only attribute like this (I haven't tested this):

    File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.ReadOnly);
    
    0 讨论(0)
  • 2020-12-01 06:39

    Setting Attributes to FileAttributes.Normal worked for me on both folders and files.

    0 讨论(0)
  • 2020-12-01 06:41
    var di = new DirectoryInfo("SomeFolder");
    di.Attributes &= ~FileAttributes.ReadOnly;
    
    0 讨论(0)
  • 2020-12-01 06:47

    And the version with everything at once if something did not work

            this._path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My App Settings");
    
            if (!Directory.Exists(this._path))
            {
                Directory.CreateDirectory(this._path);
    
                DirectoryInfo directoryInfo = new DirectoryInfo(this._path);
                directoryInfo.Attributes &= ~FileAttributes.ReadOnly;
    
                FileSystemInfo[] info = directoryInfo.GetFileSystemInfos("*", SearchOption.AllDirectories);
                for (int i = 0; i < info.Length; i++)
                {
                    info[i].Attributes = FileAttributes.Normal;
                }
            }
    
    0 讨论(0)
  • 2020-12-01 06:52

    If you're attempting to remove the attribute of a file in the file system, create an instance of the System.IO.FileInfo class and set the property IsReadOnly to false.

            FileInfo file = new FileInfo("c:\\microsoft.text");
            file.IsReadOnly = false;
    
    0 讨论(0)
  • 2020-12-01 06:54
        public static void DeleteDirectory(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);
        }
    
    0 讨论(0)
提交回复
热议问题