从目录中删除只读属性

43

我该如何使用C#编程来从目录中移除readonly属性?


@Lalit 你想要实现什么目标? - ACP
1
看到我在某个路径上有一个只读文件夹。我想要复制其中的一些文件。所以我想临时将其设置为可写,也就是移除只读属性,然后复制文件,再将该文件夹重新设置为只读。你能帮我吗?这是我的需求。 - Red Swan
2
在Windows上,将只读属性设置为目录实际上是相当无意义的。您仍然可以删除、重命名等该目录。有关更多信息,请参见 https://support.microsoft.com/en-us/help/326549/you-cannot-view-or-change-the-read-only-or-the-system-attributes-of-folders-in-windows-server-2003,-in-windows-xp,-in-windows-vista-or-in-windows-7。 - Alastair Maw
8个回答

96
var di = new DirectoryInfo("SomeFolder");
di.Attributes &= ~FileAttributes.ReadOnly;

35
&= --> 追加,因此不要触碰所有其他属性 ~ --> 补集,因此要执行与只读相反的操作 - MarkKGreenway
这可能是正确的答案。然而,正如Alastair在问题中所评论的那样,你应该在使用之前阅读https://support.microsoft.com/en-us/help/326549。它可能不会做你想要的事情。请注意,文件属性对话框上的“只读”选项说:“(仅适用于文件夹中的文件)”。 - Wallace Kelly
快速附注:在读取/更新属性之前,您需要Refresh(),因为目录信息被缓存。请参阅:https://learn.microsoft.com/en-us/dotnet/api/system.io.filesysteminfo.attributes#remarks - Ben Keene

9
这是一个关于使用C#修改文件属性的好链接:http://www.csharp-examples.net/file-attributes/。根据他们的示例,你可以像这样移除只读属性(我没有测试过):
File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.ReadOnly);

1
@Red Swan:我刚刚测试了一下添加隐藏属性的方法,对于目录也可以正常工作。 - Petrucio
1
这适用于文件和目录,因为目录就像一个带有目录属性设置(FileAttributes.Directory)的特殊文件。 - seveves

6
使用-=赋值运算符有两个危险之处:
1)它仅在ReadOnly属性设置时才有效,因此需要事先进行测试。
2)它执行减法操作,而当使用二进制标志时,减法操作不是最佳选择。如果条件1(上述内容)为真,则减法操作有效,但其他位将被修改!
使用&= ~FileAttributes.ReadOnly;命令删除ReadOnly标志。
使用|= FileAttributes.ReadOnly;命令应用ReadOnly标志。

2
如果您想在文件系统中删除一个文件的属性,请创建System.IO.FileInfo类的实例,并将属性IsReadOnly设置为false。
        FileInfo file = new FileInfo("c:\\microsoft.text");
        file.IsReadOnly = false;

1

如果某些内容无法正常工作,则可以使用一次性包含所有内容的版本。

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

将属性设置为FileAttributes.Normal对我在文件夹文件上都有效。


1
我对这种方法唯一的担忧是,如果文件夹(或文件)有其他属性怎么办?如果一个文件夹是隐藏和只读的,并且你只想让它不再是只读的,你的方法也会使它不再是隐藏的。这可能会产生意想不到的后果。 - Brian J

-1

终于搞定了。 ;)

class Program
{
    static void Main(string[] args)
    {
        DirectoryInfo di = new DirectoryInfo("c:\\test");

        FileAttributes f = di.Attributes;

        Console.WriteLine("Directory c:\\test has attributes:");
        DecipherAttributes(f);

    }

    public static void DecipherAttributes(FileAttributes f)
    {
        // To set use File.SetAttributes

        File.SetAttributes(@"C:\test", FileAttributes.ReadOnly);

        if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            Console.WriteLine("ReadOnly");

        // To remove readonly use "-="
        f -= FileAttributes.ReadOnly;

        if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            Console.WriteLine("ReadOnly");
        else
            Console.WriteLine("Not ReadOnly");
    }
}

我们想要从中移除只读标志。不仅仅是显示它是否为只读文件/文件夹! - ebrahim.mr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接