windows 使用 WinAPI 规范化文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/684684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Normalize file path with WinAPI
提问by Alex B
Possible Duplicate:
Best way to determine if two path reference to same file in C/C++
Given two file path strings with potentially different casing and slashes ('\'
vs '/'
), is there a quick way (that does not involve writing my own function) to normalize both paths to the same form, or at least to test them for equivalence?
给定两个可能具有不同大小写和斜线('\'
vs '/'
)的文件路径字符串,是否有一种快速的方法(不涉及编写我自己的函数)将两个路径规范化为相同的形式,或者至少测试它们的等效性?
I'm restricted to WinAPI and standard C++. All files are local.
我仅限于 WinAPI 和标准 C++。所有文件都是本地的。
采纳答案by mghie
Depending on whether the paths could be relative, or contain "..", or junction points, or UNC paths this may be more difficult than you think. The best way might be to use the GetFileInformationByHandle()function as in this answer.
根据路径是否可以是相对的,或包含“..”,或连接点,或 UNC 路径,这可能比您想象的更困难。最好的方法可能是使用本答案中的GetFileInformationByHandle()函数。
Edit:I agree with the comment by RBerteig that this may become hard to impossible to do if the paths are not pointing to a local file. Any comment on how to safely handle this case would be greatly appreciated.
编辑:我同意 RBerteig 的评论,即如果路径不指向本地文件,这可能变得很难甚至不可能做到。任何关于如何安全处理这种情况的评论将不胜感激。
回答by i_am_jorf
May I suggest PathCanonicalize?
我可以建议PathCanonicalize吗?
回答by steveha
I found a blog posting with the most thorough, even elaborate, function I have ever seen to solve this problem. It handles anything, even horrible corner cases like V:foo.txt
where you used the subst
command to map V:
to Z:
but you already used subst
to map Z:
to some other drive; it loops until all subst
commands are unwound. URL:
我找到了一篇博客文章,其中包含我所见过的解决此问题的最彻底、甚至最详尽的功能。它可以处理任何事情,甚至是可怕的极端情况,例如V:foo.txt
您使用subst
命令映射V:
到Z:
但您已经习惯subst
映射Z:
到其他驱动器的情况;它循环直到所有subst
命令都被解开。网址:
http://pdh11.blogspot.com/2009/05/pathcanonicalize-versus-what-it-says-on.html
http://pdh11.blogspot.com/2009/05/pathcanonicalize-versus-what-it-says-on.html
My project is pure C code, and that function is C++. I started to translate it, but then I figured out that I could get the normalized path that I wanted with one function call: GetLongPathName(). This won't handle the horrible corner cases, but it handled my immediate needs.
我的项目是纯 C 代码,该函数是 C++。我开始翻译它,但后来我发现我可以通过一个函数调用获得我想要的规范化路径:GetLongPathName()。这不会处理可怕的极端情况,但它可以满足我的直接需求。
I discovered that GetLongPathName("foo.txt")
just returns foo.txt
, but just by prepending ./
to the filename I got the expansion to normalized form:
我发现它GetLongPathName("foo.txt")
只是返回foo.txt
,但仅仅通过./
在文件名前加上我就得到了规范化形式的扩展:
GetLongPathName("./foo.txt")
, if executed in directory C:\Users\steveha
, returns C:\Users\steveha\foo.txt
.
GetLongPathName("./foo.txt")
,如果在目录中执行C:\Users\steveha
,则返回C:\Users\steveha\foo.txt
。
So, in pseudocode:
所以,在伪代码中:
if the second char of the pathname is ':' or the first char is '/' or '\', just call GetLongPathName() else, copy "./" to a temp buffer, then copy the filename to temp buffer + 2, to get a copy of the filename prepended with "./" and then call GetLongPathName().
如果路径名的第二个字符是 ':' 或第一个字符是 '/' 或 '\',只需调用 GetLongPathName() 否则,将“./”复制到临时缓冲区,然后将文件名复制到临时缓冲区 + 2 , 获取以“./”开头的文件名副本,然后调用 GetLongPathName()。
回答by Jim Mischel
There are odd cases. For example, "c:\windows..\data\myfile.txt" is the same as "c:\data.\myfile.txt" and "c:\data\myfile.txt". You can have any number of "\.\" and "\..\" in there. You might look into the Windows API function GetFullPathName
. It might do canonicalization for you.
有奇怪的情况。例如,“c:\windows..\data\myfile.txt”与“c:\data.\myfile.txt”和“c:\data\myfile.txt”相同。您可以在其中包含任意数量的“\.\”和“\..\”。您可以查看 Windows API 函数GetFullPathName
。它可能会为您进行规范化。