代码之家  ›  专栏  ›  技术社区  ›  user113476

如何清除.NET中文件的只读标志?

  •  3
  • user113476  · 技术社区  · 15 年前

    你怎么清理 只读的 .NET中文件上的标志 剩下的都完好无损 ?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Alconja    15 年前

    你不能这样做吗?

    FileInfo f = new FileInfo("yourfile.txt");
    f.IsReadOnly = false;
    

    还是我错过了什么?

        2
  •  2
  •   casperOne    15 年前

    我将获取该文件的fileinfo实例,然后将isreadonly属性设置为false(根据此处的文档: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.isreadonly.aspx )以下内容:

    new FileInfo("path").IsReadOnly = false;
    

    如果您坚持在文件类上使用static getattributes和setattributes方法,您只需执行以下操作:

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

    当要清除位图上的标志时,一般的模式是获取该标志的值(在本例中为fileattributes.readonly),将其反转(使用~operator),然后将反转的值应用于包含各种标志的值(在本例中为file.getattributes(“path”)。