2011-09-13 27 views
64

假設文件具有以下屬性:ReadOnly, Hidden, Archived, System如何刪除一個屬性?(例如只讀)如何從文件中刪除單個屬性(例如ReadOnly)?

如果我使用:

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

它會刪除所有的屬性。

+0

讀取當前屬性,掩蓋你需要設置屬性,設置屬性... –

回答

79

MSDN:您可以刪除像任何屬性,這

(但@ SLL的只是只讀的答案是隻是屬性更好)

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; 
    } 
} 
+0

Thx很多,這是更多的少,只是我需要:D – Bjqn

102

回答你的問題在標題關於ReadOnly屬性:

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

要獲得控制權的任何屬性,你依然可以使用File.SetAttributes()方法。該鏈接還提供了一個示例。

+1

這個作品大!但只適用於ReadOnly Attribute(我知道我在標題中要求它,但我還需要其他屬性) – MilMike

+1

@qxxx:您說得對,正如我所提到的,您必須使用SetAttributes()方法來修改其他屬性 – sll

+3

作爲一個-liner:new FileInfo(fileName){IsReadOnly = false} .Refresh() –

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

我不能得到.HasFlag(SOMEFLAG);上班 ? – Bjqn

+0

@Bjqn,問一個新問題! –

+0

是的,我可以,但我真的不需要知道:) – Bjqn

1

對於一個在線解決方案(前提是當前用戶具有訪問權限更改上述文件的屬性)這裏是我會怎麼做:

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"); 

參考

+0

這些不是主要的內容*改變*,它們是內容添加,並沒有違背堆棧溢出的精神。他們是很好的編輯,他們應該留下。 –

+1

這似乎相當於詢問向您的汽車添加一夸脫油的途徑,但被告知應該將其交給經銷商換油。爲什麼執行shell命令只是爲了翻轉文件屬性位?答案在技術上是有效的,所以我沒有倒下,但是我在我的心裏。 – JMD

+0

@GeorgeStocker感謝您的評論,我很感激! – tehDorf

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

例子:

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); 
} 
2
if ((oFileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) 
    oFileInfo.Attributes ^= FileAttributes.ReadOnly; 
相關問題