我的一个朋友给了我一张损坏的 SD 卡,其中将“DCIM”文件夹显示为文件。我编写了一个控制台程序来显示 FileInfo,它返回“正常”。现在我尝试将 FileAttributes 从“正常”更改为“目录”,如示例中所示:http: //msdn.microsoft.com/de-de/library/system.io.file.setattributes%28v=vs.110% 29.aspx
static void Main(string[] args)
{
var path = "O://DCIM";
FileAttributes attributes = File.GetAttributes(path);
attributes = RemoveAttribute(attributes, FileAttributes.Normal);
var attr = attributes | FileAttributes.Directory;
File.SetAttributes(path, attr);
var fi = new FileInfo(path);
Console.WriteLine(fi.Name + " -- " + fi.Attributes);
Console.ReadKey();
}
private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
程序按预期运行,当我在分配它之前检查 attr 时,它返回“目录”。但最后,FileAttributes 仍然是“Normal”。
是否可以通过这种方式更改 FileAttributes?有另一种解决方案吗?