让说,文件具有以下属性:ReadOnly, Hidden, Archived, System
。
如何仅删除一个属性?(例如ReadOnly)
如果使用以下内容,它将删除所有属性:
IO.File.SetAttributes("File.txt",IO.FileAttributes.Normal)
Answers:
从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;
}
}
~
办?
回答有关ReadOnly
属性的标题问题:
FileInfo fileInfo = new FileInfo(pathToAFile);
fileInfo.IsReadOnly = false;
要自己控制任何属性,可以使用File.SetAttributes()
方法。该链接还提供了一个示例。
对于单行解决方案(假设当前用户有权更改提到的文件的属性),这是我的方法:
VB.Net
Shell("attrib file.txt -r")
负号表示,remove
而r
表示只读。如果还要删除其他属性,则可以执行以下操作:
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");
参考文献
/// <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);
}
用这个:
private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
在MSDN中阅读详细信息:http : //msdn.microsoft.com/zh-cn/library/system.io.file.setattributes.aspx