我正在尝试使用kernel32.dll的CreateFile方法为“C:”创建一个安全的文件句柄,该方法总是返回一个无效的句柄。
我在这里做错了什么帮助?“C:
CreateFile(
lpFileName: "C:",
dwDesiredAccess: FileAccess.ReadWrite,
dwShareMode: FileShare.ReadWrite,
lpSecurityAttributes: IntPtr.Zero,
dwCreationDisposition: FileMode.OpenOrCreate,
dwFlagsAndAttributes: FileAttributes.Normal,
hTemplateFile: IntPtr.Zero);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern SafeFileHandle CreateFile(
string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
IntPtr lpSecurityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
答案 0 :(得分:4)
有几个参数不太正确。
\\.\
。试试这段代码:
SafeFileHandle handle = CreateFile(
lpFileName: @"\\.\C:",
dwDesiredAccess: FileAccess.Read,
dwShareMode: FileShare.ReadWrite,
lpSecurityAttributes: IntPtr.Zero,
dwCreationDisposition: FileMode.OpenOrCreate,
dwFlagsAndAttributes: FileAttributes.Normal,
hTemplateFile: IntPtr.Zero );
请注意,要打开具有读取权限的卷句柄,必须以管理员身份运行,否则您将被拒绝访问(错误代码5)。正如Nik Bougalis和CreateFile文档指出的那样,如果您指定dwDesiredAccess
为0,则不需要管理员权限。
如果此参数为零,则应用程序可以查询某些元数据(如文件,目录或设备属性),而无需访问该文件或设备,即使GENERIC_READ访问权限已被拒绝。
答案 1 :(得分:1)
这就是我解决问题的方法
private const int GENERIC_READ = unchecked((int)0x80000000);
private const int FILE_SHARE_READ = 1;
private const int FILE_SHARE_WRITE = 2;
private const int OPEN_EXISTING = 3;
private const int IOCTL_DISK_GET_DRIVE_LAYOUT_EX = unchecked((int)0x00070050);
private const int ERROR_INSUFFICIENT_BUFFER = 122;
NativeMethods.CreateFile("\\\\.\\PHYSICALDRIVE" + PhysicalDrive, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero))