【问题标题】:How to remove a single Attribute (e.g. ReadOnly) from a File?如何从文件中删除单个属性(例如只读)?
【发布时间】:2011-09-13 09:29:17
【问题描述】:

假设一个文件具有以下属性:ReadOnly, Hidden, Archived, System如何只删除一个属性?(例如 ReadOnly)

如果我使用以下内容,它会删除所有属性:

IO.File.SetAttributes("File.txt",IO.FileAttributes.Normal)

【问题讨论】:

  • 读取当前属性,屏蔽你需要设置的属性,设置属性...

标签: c# .net file file-attributes


【解决方案1】:

在标题中回答您关于ReadOnly 属性的问题:

FileInfo fileInfo = new FileInfo(pathToAFile);
fileInfo.IsReadOnly = false;

要自己控制任何属性,您可以使用File.SetAttributes() 方法。该链接还提供了一个示例。

【讨论】:

  • 这很好用!但仅适用于 ReadOnly 属性,(我知道我在标题中要求它,但我还需要其他属性)
  • @qxxx:你说得对,正如我提到的,你必须使用 SetAttributes() 方法来修改另一个属性
  • 单行:new FileInfo(fileName) {IsReadOnly = false}.Refresh()
  • 是否需要 Refresh()? This example on MSDN 建议它不是,如果我不调用它,我的代码(诚然与 Mono 一起使用)可以工作。
  • Refresh() 对我来说似乎也没有必要。
【解决方案2】:
string file = "file.txt";
FileAttributes attrs = File.GetAttributes(file);
if (attrs.HasFlag(FileAttributes.ReadOnly))
    File.SetAttributes(file, attrs & ~FileAttributes.ReadOnly);

【讨论】:

    【解决方案3】:

    来自MSDN:你可以像这样删除任何属性

    (但@sll 对 ReadOnly 的回答更适合该属性)

    using System;
    using System.IO;
    using System.Text;
    class Test 
    {
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";
    
            // Create the file if it exists.
            if (!File.Exists(path)) 
            {
                File.Create(path);
            }
    
            FileAttributes attributes = File.GetAttributes(path);
    
            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                // Make the file RW
                attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
                File.SetAttributes(path, attributes);
                Console.WriteLine("The {0} file is no longer RO.", path);
            } 
            else 
            {
                // Make the file RO
                File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
                Console.WriteLine("The {0} file is now RO.", path);
            }
        }
    
        private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
        {
            return attributes & ~attributesToRemove;
        }
    }
    

    【讨论】:

    【解决方案4】:

    如果您想要一个简单的解决方案(没有 Shell),这些示例中有很多代码:

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

    【讨论】:

      【解决方案5】:
      if ((oFileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
          oFileInfo.Attributes ^= FileAttributes.ReadOnly;
      

      【讨论】:

        【解决方案6】:
        /// <summary>
        /// Addes the given FileAttributes to the given File.
        /// It's possible to combine FileAttributes: FileAttributes.Hidden | FileAttributes.ReadOnly
        /// </summary>
        public static void AttributesSet(this FileInfo pFile, FileAttributes pAttributes)
        {
            pFile.Attributes = pFile.Attributes | pAttributes;
            pFile.Refresh();
        }
        
        /// <summary>
        /// Removes the given FileAttributes from the given File.
        /// It's possible to combine FileAttributes: FileAttributes.Hidden | FileAttributes.ReadOnly
        /// </summary>
        public static void AttributesRemove(this FileInfo pFile, FileAttributes pAttributes)
        {
            pFile.Attributes = pFile.Attributes & ~pAttributes;
            pFile.Refresh();
        }
        
        /// <summary>
        /// Checks the given File on the given Attributes.
        /// It's possible to combine FileAttributes: FileAttributes.Hidden | FileAttributes.ReadOnly
        /// </summary>
        /// <returns>True if any Attribute is set, False if non is set</returns>
        public static bool AttributesIsAnySet(this FileInfo pFile, FileAttributes pAttributes)
        {
            return ((pFile.Attributes & pAttributes) > 0);
        }
        
        /// <summary>
        /// Checks the given File on the given Attributes.
        /// It's possible to combine FileAttributes: FileAttributes.Hidden | FileAttributes.ReadOnly
        /// </summary>
        /// <returns>True if all Attributes are set, False if any is not set</returns>
        public static bool AttributesIsSet(this FileInfo pFile, FileAttributes pAttributes)
        {
            return (pAttributes == (pFile.Attributes & pAttributes));
        }
        

        例子:

        private static void Test()
        {
            var lFileInfo = new FileInfo(@"C:\Neues Textdokument.txt");
            lFileInfo.AttributesSet(FileAttributes.Hidden | FileAttributes.ReadOnly);
            lFileInfo.AttributesSet(FileAttributes.Temporary);
            var lBool1 = lFileInfo.AttributesIsSet(FileAttributes.Hidden);
            var lBool2 = lFileInfo.AttributesIsSet(FileAttributes.Temporary);
            var lBool3 = lFileInfo.AttributesIsSet(FileAttributes.Encrypted);
            var lBool4 = lFileInfo.AttributesIsSet(FileAttributes.ReadOnly | FileAttributes.Temporary);
            var lBool5 = lFileInfo.AttributesIsSet(FileAttributes.ReadOnly | FileAttributes.Encrypted);
            var lBool6 = lFileInfo.AttributesIsAnySet(FileAttributes.ReadOnly | FileAttributes.Temporary);
            var lBool7 = lFileInfo.AttributesIsAnySet(FileAttributes.ReadOnly | FileAttributes.Encrypted);
            var lBool8 = lFileInfo.AttributesIsAnySet(FileAttributes.Encrypted);
            lFileInfo.AttributesRemove(FileAttributes.Temporary);
            lFileInfo.AttributesRemove(FileAttributes.Hidden | FileAttributes.ReadOnly);
        }
        

        【讨论】:

          【解决方案7】:

          对于单行解决方案(前提是当前用户有权更改上述文件的属性),我会这样做:

          VB.Net

          Shell("attrib file.txt -r")
          

          负号表示remover 是只读的。 如果你也想删除其他属性,你会这样做:

          Shell("attrib file.txt -r -s -h -a")
          

          这将删除只读、系统文件、隐藏和存档属性。

          如果您想返回这些属性,方法如下:

          Shell("attrib file.txt +r +s +h +a")
          

          顺序无关紧要。

          C#

          Process.Start("cmd.exe", "attrib file.txt +r +s +h +a");
          

          参考文献

          【讨论】:

          • 这些不是主要的内容变化,它们是内容的增加,没有一个与 Stack Overflow 的精神背道而驰。他们是很好的编辑,他们应该留下来。
          • 这似乎相当于询问在哪里为您的汽车添加一夸脱机油,但被告知您应该将其带到经销商处更换机油。为什么执行 shell 命令只是为了翻转文件属性位?答案在技术上是有效的,所以我没有投反对票,但我在心里投了赞成票。
          • @GeorgeStocker 感谢您的审阅,非常感谢!
          【解决方案8】:

          使用这个:

          private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
          

          在 MSDN 中阅读详细信息:http://msdn.microsoft.com/en-us/library/system.io.file.setattributes.aspx

          【讨论】:

            猜你喜欢
            • 2011-12-16
            • 1970-01-01
            • 2010-10-27
            • 2018-04-19
            • 2011-01-19
            • 2011-01-30
            • 1970-01-01
            • 2010-09-20
            相关资源
            最近更新 更多