1

我的一个朋友给了我一张损坏的 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?有另一种解决方案吗?

4

1 回答 1

1

根据SetFileAttributes API 函数的文档,调用File.SetAttributes解析为:

文件不能转换为目录。

如果您想从该设备获取信息,则必须使用低级设备 I/O 读取原始扇区。这是可能的,但工作量很大。

如果您对从该设备获取信息是认真的,请不要尝试对其进行写入。数据已损坏。尝试修改该设备上的数据更有可能进一步破坏那里的数据。使用低级阅读器读取硬盘驱动器上文件的二进制图像。然后制作该文件的副本,您可以在副本上敲自己。

当然,您可能想问自己,该驱动器上的数据是否真的值得您为尝试恢复它而付出的努力。数据恢复可能非常乏味。

于 2014-11-25T22:11:49.950 回答