C# 如何从文件中删除单个属性(例如只读)?

C# 如何从文件中删除单个属性(例如只读)?,c#,.net,file,file-attributes,C#,.net,File,File Attributes,比如说,文件具有以下属性:只读、隐藏、存档、系统。 如何仅删除一个属性?(例如ReadOnly) 如果使用以下选项,则会删除所有属性: IO.File.SetAttributes("File.txt",IO.FileAttributes.Normal) 回答标题中有关ReadOnly属性的问题: FileInfo fileInfo = new FileInfo(pathToAFile); fileInfo.IsReadOnly = false; 要自己控制任何属性,可以使用方法。该链接还提供

比如说,文件具有以下属性:
只读、隐藏、存档、系统
如何仅删除一个属性?(例如ReadOnly)

如果使用以下选项,则会删除所有属性:

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

回答标题中有关
ReadOnly
属性的问题:

FileInfo fileInfo = new FileInfo(pathToAFile);
fileInfo.IsReadOnly = false;
要自己控制任何属性,可以使用方法。该链接还提供了一个示例。

From:您可以删除任何类似这样的属性

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

使用以下命令:

private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
请阅读MSDN:

中有关单行解决方案的详细信息(前提是当前用户有权更改所述文件的属性),我将如何执行此操作:

VB.Net

Shell("attrib file.txt -r")
负号表示
删除
,而
r
表示只读。 如果您也想删除其他属性,请执行以下操作:

Shell("attrib file.txt -r -s -h -a")
这将删除只读、系统文件、隐藏和归档属性

如果要返回这些属性,请执行以下操作:

Shell("attrib file.txt +r +s +h +a")
顺序无关紧要

C#

参考资料

  • -大量示例和特定于操作系统的注释
  • -页面适用于XP,但可能适用于更高版本
  • -特别是

如果您想要一个简单的解决方案(没有外壳),那么这些示例中有大量代码:


阅读当前属性,屏蔽您需要设置的属性,设置属性…这非常有效!但是只针对ReadOnly属性(我知道我在标题中要求了它,但我还需要其他属性)@qxxx:你是对的,正如我提到的,你必须使用SetAttributes()方法修改另一个属性作为一行:new FileInfo(fileName){IsReadOnly=false}。Refresh()需要Refresh()吗?如果我不调用它,我的代码(当然是Mono代码)就可以工作。Refresh()似乎对我来说也不是必需的。这些不是主要的内容更改,它们是内容添加,并且没有一个与堆栈溢出的精神背道而驰。他们是很好的编辑,他们应该留下来。这似乎相当于问在哪里给你的车加一夸脱油,但被告知你应该把它带到你的经销商那里换油。为什么执行shell命令只是为了翻转文件属性位?这个答案在技术上是有效的,所以我没有投反对票,但我在心里投了反对票。@GeorgeStocker谢谢你的审阅,我很感激!
~
做什么?@newbiegay:这是一元二进制补码运算符(也称为按位not运算符)。
Shell("attrib file.txt -r -s -h -a")
Shell("attrib file.txt +r +s +h +a")
Process.Start("cmd.exe", "attrib file.txt +r +s +h +a");
/// <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);
}
if ((oFileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    oFileInfo.Attributes ^= FileAttributes.ReadOnly;
File.SetAttributes(filepath, ~FileAttributes.ReadOnly & File.GetAttributes(filepath));