Why does my canonicalized path get prefixed with \\?\

杀马特。学长 韩版系。学妹 提交于 2020-01-05 02:27:31

问题


I'm working on a personal project that I was trying to solve via canonicalizing a relative path in Rust. However, whenever I do so, the new path gets prefixed with a strange \\?\ sequence. For example, something as simple as:

let p = fs::canonicalize(".").unwrap();
println!("{}", p.display());

will result in something like the following output:

\\?\C:\Users\[...]\rustprojects\projectname

This isn't a particular problem because I can accomplish what I'm attempting in other ways. However, it seems like odd behavior, especially if you are going to use the string form of the path in some way that requires accuracy. Why is this sequence of characters prepending the result, and how can I avoid it?


回答1:


The \\?\ prefix tells Windows to treat the path as is, i.e. it disables the special meaning of . and .., special device names like CON are not interpreted and the path is assumed to be absolute. It also enables using paths up to 32,767 characters (UTF-16 code units), whereas otherwise the limit is 260 (unless you're on Windows 10, version 1607 or later, and your application opts in to longer paths).

Therefore, the \\?\ prefix ensures that you'll get a usable path; removing that prefix may yield a path that is unusable or that resolves to a different file! As such, I would recommend that you keep that prefix in your paths.



来源:https://stackoverflow.com/questions/41233684/why-does-my-canonicalized-path-get-prefixed-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!