gpt4 book ai didi

c# - CreateFile、FILE_FLAG_BACKUP_SEMANTICS、目录管理和句柄

转载 作者:行者123 更新时间:2023-11-28 05:44:01 29 4
gpt4 key购买 nike

我正在尝试获取 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 显然包含正确的标志。收到的编译器错误是:

cannot convert from 'uint' to 'System.IO.FileAttributes'

这个错误是有道理的;我只是不确定我能够对签名进行哪些按摩,因为我是 extern 函数的新手。

是否有与所需标志对应的 FileAttributes?我需要更改 extern 签名吗?

最佳答案

在您的代码中包含此枚举定义:

[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中定义的文件属性不包含很多属性。

关于c# - CreateFile、FILE_FLAG_BACKUP_SEMANTICS、目录管理和句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36600470/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com