相对路径转绝对路径(Win)

绝对路径转相对路径可以使用:PathRelativePathTo
相对路径转绝对路径好像没有现成的。

可以考虑 GetCurrentDirectory  GetModuleFileName  PathStripPath 等。

问题描述:需要根据Path来判断它属于那个分区。但是不支持相对路径。
目标:    支持相对路径和绝对路径。
解决方法:

char GetDriveName(LPWSTR lpszPath)
{
    //we assume that the lpszPath passed in is valid, if not, Results Not Guaranteed.
    WCHAR szBuffer[MAX_PATH] = {0};
    
    if(PathIsRelative(lpszPath))
        GetCurrentDirectoryW(MAX_PATH, szBuffer);
    else
        wcscpy_s(szBuffer, MAX_PATH, lpszPath);

    return PathGetDriveNumberW(szBuffer)+'A';
}

void main()
{
    const int ARRAY_LEN = 20;
    WCHAR szPath[][ARRAY_LEN] = { L".\GotData.mp3", L"D:\WinStuff.zip"};
    int len = sizeof(szPath) / sizeof(szPath[0]);

    for(int i = 0; i < len; i++)
    {
        wcout<<L"Path:	"<<szPath[i]<<endl;
        wcout<<L"Drive Name:	"<<GetDriveName(szPath[i])<<endl;
    }
    system("pause");
}

测试结果:

Path:   .GotData.mp3
Drive Name:     E
Path:   D:WinStuff.zip
Drive Name:     D
请按任意键继续. . .