如何按名称打开卷

How to open volume by name

我需要按名称打开卷(获取卷句柄)。目标卷名称是 "\?\Volume{A25CF44F-8CB3-46E7-B3A7-931385FDF8CB}\" 并且这应该有效。根据CreateFile function

You can also open a volume by referring to its volume name.

C#代码:

public static class Wrapper
{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
     public static extern SafeFileHandle CreateFile(
     [MarshalAs(UnmanagedType.LPTStr)] string filename,
     [MarshalAs(UnmanagedType.U4)] FileAccess access,
     [MarshalAs(UnmanagedType.U4)] FileShare share,
     IntPtr securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
     [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
     [MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
     IntPtr templateFile);
}

    static void Main(string[] args)
    {
        string sourceVol = @"\?\Volume{A25CF44F-8CB3-46E7-B3A7-931385FDF8CB}\";

        SafeFileHandle sourceVolHandle = Wrapper.CreateFile(sourceVol, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
        if (sourceVolHandle.IsInvalid)
            throw new Win32Exception(); //Here I got "The system cannot find the path specified"

那么如何按名称打开卷? (我知道我可以使用盘符“\\.\C:”打开卷,但这是不可接受的)

要打开交易量,我们需要删除结尾的斜杠,因此:

string sourceVol = @"\?\Volume{A25CF44F-8CB3-46E7-B3A7-931385FDF8CB}";