简体   繁体   中英

How to open volume by name

I need to open volume by name(get volume handle). The target volume name is "\\?\\Volume{A25CF44F-8CB3-46E7-B3A7-931385FDF8CB}\\" and this should works. According to CreateFile function

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

The C# code:

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"

So how to open volume by name? (I know that I can open volume using drive letter "\\\\.\\C:" but that is not accepteble)

要打开音量,我们需要删除斜杠,因此:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM