将path转换为它的GUID形式

本文关键字:GUID 形式 path 转换 | 更新日期: 2023-09-27 18:19:25

我需要将path(例如C:')转换为该卷的GUID形式(例如'''?'Volume{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}')

c#中有这样的函数吗?

将path转换为它的GUID形式

您需要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);