我试图抓取directory handle以检索目录的标识符。文档(上面链接)指定需要将FILE_FLAG_BACKUP_SEMANTICS
标志传递给CreateFile
函数以便检索句柄本身。
但是,在咨询pinvoke's kernel32.dll
个签名时,大多数C#候选人的内容如下:
[DllImport("kernell32.dll", SetLastError = true)]
public static extern SafeFileHandle CreateFile(
string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile
);
上述一对一参数映射到C ++ CreateFile意味着dwFlagsAndAttributes
参数是FILE_FLAG_BACKUP_SEMANTICS
的占位符;但是,FileAttribute
枚举似乎与该标志不匹配。
现在(它已经坏了)我的电话看起来像是:
var createdFolder =
FileSystemInteractor.CreateFile(
fullPathWithFolderName,
FileAccess.Read,
FileShare.Read,
IntPtr.Zero,
FileMode.Open,
Kernel32.FILE_FLAG_BACKUP_SEMANTICS,
IntPtr.Zero
);
Kernel32
显然包含正确的标志。收到的编译器错误是:
无法从'uint'转换为'System.IO.FileAttributes'
错误有意义;我只是不确定我能够做什么的按摩,因为我是extern
函数的新手。
是否有FileAttributes
对应于所需的标志?我是否需要更改extern
签名?
答案 0 :(得分:1)
在您的代码中包含此枚举定义:
[Flags]
private enum File_Attributes : uint
{
Readonly = 0x00000001,
Hidden = 0x00000002,
System = 0x00000004,
Directory = 0x00000010,
Archive = 0x00000020,
Device = 0x00000040,
Normal = 0x00000080,
Temporary = 0x00000100,
SparseFile = 0x00000200,
ReparsePoint = 0x00000400,
Compressed = 0x00000800,
Offline = 0x00001000,
NotContentIndexed = 0x00002000,
Encrypted = 0x00004000,
Write_Through = 0x80000000,
Overlapped = 0x40000000,
NoBuffering = 0x20000000,
RandomAccess = 0x10000000,
SequentialScan = 0x08000000,
DeleteOnClose = 0x04000000,
BackupSemantics = 0x02000000,
PosixSemantics = 0x01000000,
OpenReparsePoint = 0x00200000,
OpenNoRecall = 0x00100000,
FirstPipeInstance = 0x00080000
}
System.IO中定义的文件属性不包含许多属性。