4

我试图直接用 FileStream 打开 C: 没有成功:

new FileStream("C:", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

System.UnauthorizedAccessException 未处理

Message="拒绝访问路径 'C:\'。"

来源="mscorlib"

堆栈跟踪:

  in  System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

  in  System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

  in  System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)

  in  System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)

  in  ReadingMftNewTest.Program.Main(String[] args) in D:\CS\2008\ReadingMftNewTest\ReadingMftNewTest\Program.cs:line 76

请注意,我打开“C:”但错误显示“C:\”,这个斜杠是从哪里来的?:\

有没有机会在不使用CreateFileAPI的情况下打开 C:?

我真的不想依赖于 WIN32 API,因为此代码也应该在不支持 WIN32 API 的 Mono 上运行,但可以使用常规 FileStream(Mono 1 Microsoft 0)成功打开设备。

4

3 回答 3

2

我终于找到了一种方法来做到这一点:

new FileStream(@"C:\$Volume", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

这仅适用于 NTFS 卷。

于 2010-03-13T07:37:15.067 回答
1

当您引用名称中没有 的根驱动器时\,您使用的就是文件系统中的别名。它映射到该根驱动器下使用的最后一个工作目录。这相当于在cmd 窗口中输入d:或输入。c:它会将您移动到最后一个目录下的相应根驱动器。

在这种情况下,c:驱动器上使用的最后一个路径是c:\. 因此,当您打开时,c:您最终会打开c:\

FileStream使用我知道的 API 无法避免这种“别名” 。所有的FileStreamAPI 最终都会Path.NormalizePath在调用 CreateFile 之前映射给定的路径。这是进行映射的函数。

于 2010-02-13T17:37:09.390 回答
1

打开驱动器需要驱动器名称,例如“\\.\PhysicalDrive0”。查找驱动器名称需要 QueryDosDevice()。Windows 允许这样做的可能性很小

于 2010-02-13T17:43:30.600 回答