- 1 year ago
How to check if two char* file-paths are the same on Windows?
To check if two file paths represented as char*
strings are the same on Windows, you can use the PathCchCompareEx
function from the Windows API. Here's an example of how you can use it:
c#include <Windows.h>
#include <PathCch.h>
int main() {
const char* path1 = "C:\\folder\\file.txt";
const char* path2 = "C:\\folder\\file.txt";
int result = PathCchCompareEx(path1, path2, PATHCCH_NONE, 0);
if (result == 0) {
printf("The paths are the same.\n");
} else {
printf("The paths are different.\n");
}
return 0;
}
Make sure to include the necessary header files and link against the appropriate libraries (PathCch.lib
for this case). Also, note that this function is available starting from Windows 8 and Windows Server 2012, so it may not be available on older Windows versions.
Keep in mind that using raw char*
strings for file paths can be error-prone, especially when dealing with special characters or path separators. It is recommended to use safer alternatives like wchar_t*
and the corresponding wide-character functions (PathCchCompareExW
) to handle file paths in Windows.