Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

path

winapi

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:

_tcsicmp(szPath1, szPath2) == 0

For example:

  • forward slashes / backslashes

  • relative / absolute paths.

[Edit] Title changed to match an existing C# question.

like image 387
Adam Tegen Avatar asked Feb 18 '09 20:02

Adam Tegen


People also ask

What does \\ mean in Windows path?

They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory, or double dots to represent the parent directory.

How do I determine a file path?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I find the relative path of a file in Windows?

Example. If the current directory is C:\Windows, the relative path to the executable for the game of Solitaire, which is located in the current directory, is simply the name of the executable – sol.exe. If the current directory is C:\Windows\System, the relative path to Solitaire is .. \sol.exe.

What are the two types of file paths?

File paths specify the location of individual files. They are used to give files access to one another and they are of two types : Absolute and Relative. Relative file paths on the hand points to the location of files in the root folder of an individual web project with reference to the current working file.


1 Answers

Open both files with CreateFile, call GetFileInformationByHandle for both, and compare dwVolumeSerialNumber, nFileIndexLow, nFileIndexHigh. If all three are equal they both point to the same file:

GetFileInformationByHandle function

BY_HANDLE_FILE_INFORMATION Structure

like image 134
MSN Avatar answered Oct 12 '22 18:10

MSN