为什么我的规范化路径以prefix为前缀? [英] Why does my canonicalized path get prefixed with \\?\

查看:140
本文介绍了为什么我的规范化路径以prefix为前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个个人项目,该项目试图通过规范化Rust中的相对路径来解决。但是,每当我这样做时,新路径都会以奇怪的 \\?\ 序列作为前缀。例如,简单的事情就是:

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?

推荐答案

\\?\ 前缀告诉Windows按原样对待路径,即禁用的特殊含义。 。不会解释特殊设备名称,例如 CON ,并且假定该路径为绝对路径。它还允许使用向上的路径到32,767个字符(UTF-16代码单元),否则限制为260 (除非您使用的是Windows 10版本1607或更高版本,并且您的应用程序选择了更长的路径)。

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.

这篇关于为什么我的规范化路径以prefix为前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆