How can I obtain the case-sensitive path on Windows?

后端 未结 8 659
慢半拍i
慢半拍i 2020-12-01 18:00

I need to know which is the real path of a given path.

For example:

The real path is: d:\\src\\File.txt
And the user give me: D:\\src\\file.txt
I nee

8条回答
  •  醉话见心
    2020-12-01 18:11

    The way to get the actual path of a file (this won't work for folders) is to follow these steps:

    1. Call CreateFileMapping to create a mapping for the file.
    2. Call GetMappedFileName to get the name of the file.
    3. Use QueryDosDevice to convert it to an MS-DOS-style path name.

    If you feel like writing a more robust program that also works with directories (but with more pain and a few undocumented features), follow these steps:

    1. Get a handle to the file/folder with CreateFile or NtOpenFile.
    2. Call NtQueryObject to get the full path name.
    3. Call NtQueryInformationFile with FileNameInformation to get the volume-relative path.
    4. Using the two paths above, get the component of the path that represents the volume itself. For example, if you get \Device\HarddiskVolume1\Hello.txt for the first path and \Hello.txt for the second, you now know the volume's path is \Device\HarddiskVolume1.
    5. Use either the poorly-documented Mount Manager I/O Control Codes or QueryDosDevice to convert substitute the volume portion of the full NT-style path with the drive letter.

    Now you have the real path of the file.

提交回复
热议问题