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

时间:2022-03-30 16:40:35

Let say, a file has the following attributes: ReadOnly, Hidden, Archived, System. How can I remove only one Attribute? (for example ReadOnly)

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

If I use:

如果我使用:

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

it removes all the attributes.

它删除了所有的属性。

7 个解决方案

#1


83  

From MSDN: You can remove any attribute like this

从MSDN:您可以删除任何这样的属性。

(but @sll's answer for just ReadOnly is better for just that attribute)

(但是@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;
    }
}

#2


104  

Answering on your question in title regarding ReadOnly attribute:

回答关于ReadOnly属性的题目:

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

To get control over any attribute yourself you can use File.SetAttributes() method. The link also provides an example.

为了获得对任何属性的控制,您可以使用File.SetAttributes()方法。该链接还提供了一个示例。

#3


11  

string file = "file.txt";
FileAttributes attrs = File.GetAttributes(file);
if (attrs.HasFlag(FileAttributes.ReadOnly))
    File.SetAttributes(file, attrs & ~FileAttributes.ReadOnly);

#4


2  

if ((oFileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    oFileInfo.Attributes ^= FileAttributes.ReadOnly;

#5


1  

For a one line solution (provided that the current user has access to change the attributes of the mentioned file) here is how I would do it:

对于一个行解决方案(如果当前用户可以访问更改上述文件的属性),我将这样做:

VB.Net

VB.Net

Shell("attrib file.txt -r")

the negative sign means to remove and the r is for read-only. if you want to remove other attributes as well you would do:

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

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

That will remove the Read-Only, System-File, Hidden and Archive attributes.

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

if you want to give back these attributes, here is how:

如果你想要归还这些属性,下面是方法:

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

the order does not matter.

顺序无关紧要。

C#

c#

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

References

引用

  • Google
  • 谷歌
  • ComputerHope.com - Lots of examples and OS specific notes
  • 大量的例子和操作系统的详细说明。
  • Microsoft - Page is for XP, but probably applies to later versions
  • Microsoft - Page适用于XP,但可能适用于以后的版本。
  • Wikipedia - Particularly the Particularities section
  • *,特别是特殊性部分。

#6


1  

/// <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));
}

Example:

例子:

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


0  

Use this:

用这个:

private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)

Read detail here in MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.setattributes.aspx

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

#1


83  

From MSDN: You can remove any attribute like this

从MSDN:您可以删除任何这样的属性。

(but @sll's answer for just ReadOnly is better for just that attribute)

(但是@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;
    }
}

#2


104  

Answering on your question in title regarding ReadOnly attribute:

回答关于ReadOnly属性的题目:

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

To get control over any attribute yourself you can use File.SetAttributes() method. The link also provides an example.

为了获得对任何属性的控制,您可以使用File.SetAttributes()方法。该链接还提供了一个示例。

#3


11  

string file = "file.txt";
FileAttributes attrs = File.GetAttributes(file);
if (attrs.HasFlag(FileAttributes.ReadOnly))
    File.SetAttributes(file, attrs & ~FileAttributes.ReadOnly);

#4


2  

if ((oFileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    oFileInfo.Attributes ^= FileAttributes.ReadOnly;

#5


1  

For a one line solution (provided that the current user has access to change the attributes of the mentioned file) here is how I would do it:

对于一个行解决方案(如果当前用户可以访问更改上述文件的属性),我将这样做:

VB.Net

VB.Net

Shell("attrib file.txt -r")

the negative sign means to remove and the r is for read-only. if you want to remove other attributes as well you would do:

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

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

That will remove the Read-Only, System-File, Hidden and Archive attributes.

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

if you want to give back these attributes, here is how:

如果你想要归还这些属性,下面是方法:

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

the order does not matter.

顺序无关紧要。

C#

c#

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

References

引用

  • Google
  • 谷歌
  • ComputerHope.com - Lots of examples and OS specific notes
  • 大量的例子和操作系统的详细说明。
  • Microsoft - Page is for XP, but probably applies to later versions
  • Microsoft - Page适用于XP,但可能适用于以后的版本。
  • Wikipedia - Particularly the Particularities section
  • *,特别是特殊性部分。

#6


1  

/// <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));
}

Example:

例子:

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


0  

Use this:

用这个:

private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)

Read detail here in MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.setattributes.aspx

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