Open C: Directly with `FileStream` without API 'CreateFile`

I am trying to open C: directly with FileStream without success:

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

System.UnauthorizedAccessException error was unhandled

Message = "Access to path" C: \ "denied."

Source = "mscorlib"

Stacktrace:

  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

Note that I open "C:", but the error says "C: \", where did this slash come from?: \

Is there a chance to open C: without using the API CreateFile?

I really do not want to be dependent on the WIN32 API, because this code should also work on Mono, which does not support the WIN32 API, but successfully opens devices with the usual FileStream (Mono 1 Microsoft 0).

+3
3

- :

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

NTFS.

+2

\ , , . , . d: c: cmd. .

, c:, c:\. c: c:\.

"", API FileStream, . API- FileStream , Path.NormalizePath, CreateFile. , .

+1

Opening a drive requires a drive name, for example, "\\. \ PhysicalDrive0". Finding a drive name requires QueryDosDevice (). The likelihood that Windows will allow this is pretty minimal .

+1
source

Source: https://habr.com/ru/post/1732679/


All Articles