将路径转换为其GUID形式

Pwe*_*asd 3 c# guid filepath

我需要将路径(例如C:\)转换为该卷的GUID表单(例如\\\?\Volume{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\).

C#中有没有这样的功能呢?

Cor*_*son 7

你需要P/Invoke GetVolumeNameForVolumeMountPoint:

static string GetVolumeGuidPath(string mountPoint)
{
    StringBuilder sb = new StringBuilder(50);
    GetVolumeNameForVolumeMountPoint(mountPoint, sb, 50);
    return sb.ToString();
}

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetVolumeNameForVolumeMountPoint(
    string lpszVolumeMountPoint,
    [Out] StringBuilder lpszVolumeName,
    int cchBufferLength);
Run Code Online (Sandbox Code Playgroud)