Best way to determine if two path reference to same file in Windows?

后端 未结 11 1648
生来不讨喜
生来不讨喜 2020-11-30 11:23

How would I compare 2 strings to determine if they refer to the same path in Win32 using C/C++?

While this will handle a lot of cases it misses some things:

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 11:41

    use the GetFullPathName from kernel32.dll, this will give you the absolute path of the file. Then compare it against the other path that you have using a simple string compare

    edit: code

    TCHAR buffer1[1000];
    TCHAR buffer2[1000];
    TCHAR buffer3[1000];
    TCHAR buffer4[1000];
    
    GetFullPathName(TEXT("C:\\Temp\\..\\autoexec.bat"),1000,buffer1,NULL);
    GetFullPathName(TEXT("C:\\autoexec.bat"),1000,buffer2,NULL);
    GetFullPathName(TEXT("\\autoexec.bat"),1000,buffer3,NULL);
    GetFullPathName(TEXT("C:/autoexec.bat"),1000,buffer4,NULL);
    _tprintf(TEXT("Path1: %s\n"), buffer1);
    _tprintf(TEXT("Path2: %s\n"), buffer2);
    _tprintf(TEXT("Path3: %s\n"), buffer3);
    _tprintf(TEXT("Path4: %s\n"), buffer4);
    

    the code above will print the same path for all three path representations.. you might want to do a case insensitive search after that

提交回复
热议问题