Windows路径中多个反斜杠的已记录行为 [英] Documented behavior for multiple backslashes in Windows paths

查看:345
本文介绍了Windows路径中多个反斜杠的已记录行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,Windows(或Windows的至少一部分)会忽略路径中的多个反斜杠,并将其视为单个反斜杠.例如,从命令提示符处或运行"窗口中执行以下任何命令都会打开记事本:

Apparently, Windows (or at least some part of Windows) ignores multiple backslashes in a path and treats them as a single backslash. For example, executing any of these commands from a command prompt or the Run window opens Notepad:

C:\Windows\System32\Notepad.exe
C:\Windows\System32\\Notepad.exe
C:\Windows\System32\\\Notepad.exe
C:\Windows\System32\\\\Notepad.exe
C:\\Windows\\System32\\Notepad.exe
C:\\\Windows\\\System32\\\Notepad.exe

这甚至可以与在命令行上传递的参数一起使用:

This can even work with arguments passed on the command line:

notepad "C:\Users\username\Desktop\\\\myfile.txt"

此行为在任何地方都有记录吗?我尝试了几次搜索,仅发现

Is this behavior documented anywhere? I tried several searches, and only found this SO question that even mentions the behavior.

注意:我不是在问UNC路径(\\服务器名),\\?\前缀或\\双引号转义符.

Note: I am not asking about UNC paths (\\servername), the \\?\ prefix, or the \\" double-quote escape.

注意:在使用批处理文件时,我偶然发现了这种现象.批处理文件中的一行看起来像这样:

Note: I stumbled upon this behavior while working with a batch file. One line in the batch file looked something like this:

"%SOME_PATH%\myapp.exe"

变量扩展后,命令如下所示:

After variable expansion, the command looked like:

"C:\Program Files\Vendor\MyApp\\myapp.exe"

令我惊讶的是,该批处理文件按预期执行,并且未因某种找不到路径"错误而失败.

To my surprise, the batch file executed as desired and did not fail with some kind of "path not found" error.

推荐答案

在大多数情况下,Win32 API函数将接受路径名格式的各种变化,包括根据当前路径将相对路径转换为绝对路径.目录或每个驱动器的当前目录,将一个点解释为此目录",将两个点解释为父目录",将正斜杠转换为反斜杠,并删除多余的反斜杠和尾随句点.

In most cases, Win32 API functions will accept a wide range of variations in the path name format, including converting a relative path into an absolute path based on the current directory or per-drive current directory, interpreting a single dot as "this directory" and two dots as "the parent directory", converting forward slashes into backslashes, and removing extraneous backslashes and trailing periods.

类似

c:\documents\..\code.\\working\.\myprogram\\\runme.exe..

将解释为

c:\code\working\myprogram\runme.exe

其中有些是记录,有些则没有. (正如汉斯所指出的那样,记录这种变通方法是错误的.)

Some of this is documented, some is not. (As Hans points out, documenting this sort of workaround legitimizes doing it wrong.)

请注意,这适用于Win32 API,不一定适用于每个应用程序甚至每个系统组件.特别是,命令解释器在处理长路径时具有更严格的规则,并且Explorer将不接受点或双点,并且通常将不接受正斜杠.另外,如果服务器未运行Windows,则对于网络驱动器,规则可能会有所不同.

Note that this applies to the Win32 API, not necessarily to every application, or even every system component. In particular, the command interpreter has stricter rules when dealing with a long path, and Explorer will not accept the dot or double-dot and typically will not accept forward slashes. Also, the rules may be different for network drives if the server is not running Windows.

这篇关于Windows路径中多个反斜杠的已记录行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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