简体   繁体   English

驱动器号和冒号后没有斜杠的 Windows 路径 - 它指向什么?

[英]Windows path with no slash after drive letter and colon - what does it point to?

I have mistyped a path and instead of c:\\foo.txt wrote c:foo.txt .我输入了错误的路径,而不是c:\\foo.txt写了c:foo.txt I expected it to either fail or to resolve to c:\\foo.txt , but instead it seems to be resolved to foo.txt in a current user's home folder.我预计它要么失败,要么解析为c:\\foo.txt ,但它似乎在当前用户的主文件夹中解析为foo.txt

Powershell returns: Powershell 返回:

PS C:\> [System.IO.Path]::GetFullPath("c:\foo.txt")
c:\foo.txt
PS C:\> [System.IO.Path]::GetFullPath("c:foo.txt")
C:\Users\Administrator\foo.txt
PS C:\> [System.IO.Path]::GetFullPath("g:foo.txt")
G:\foo.txt

Running explorer.exe from commandline and passing it any of the above results in C:\\Users\\Administrator\\Documents to be opened.从命令行运行 explorer.exe 并将上述任何结果传递给 C:\\Users\\Administrator\\Documents 以打开。

I haven't found any documentation of that and I'm utterly confused, please explain the behaviour.我还没有找到任何相关的文档,我很困惑,请解释这种行为。

This is standard DOS/Windows behavior and has always been like this.这是标准的 DOS/Windows 行为,并且一直如此。 Open a command line and see:打开命令行并查看:

C:\Users\Tim>d:              # change current drive to d:
D:\>c:                       # change back to c: - back in the same directory
C:\Users\Tim>cd d:\users     # change current directory ON D:
C:\Users\Tim>cd \            # still same directory - backslash leads to top dir
C:\>d:                       # change current drive to d:
D:\Users>                    # notice that we're now in the directory D:\Users

The drive letter always references the current directory of that drive;驱动器号总是引用该驱动器的当前目录; the (leading) backslash gets you to the top directory. (前导)反斜杠将您带到顶级目录。

When you specify a path with a drive letter but no initial backslash, it is typically interpreted as a relative path to the current directory on the specified drive.当您指定一个带有驱动器号但没有初始反斜杠的路径时,它通常被解释为指定驱动器上当前目录的相对路径。 In particular, this is how ordinary Win32 API file functions will interpret it;特别是,普通的 Win32 API 文件函数就是这样解释它的; therefore, most software which passes unmodified file paths to Win32 file functions will also behave this way.因此,大多数将未修改的文件路径传递给 Win32 文件函数的软件也会以这种方式运行。

On my machine, this works as expected in PowerShell, except for one complication:在我的机器上,这在 PowerShell 中按预期工作,除了一个并发症:

C:\Users\social>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\social> [System.IO.Path]::GetFullPath("c:foo.txt")
C:\Users\social\foo.txt
PS C:\Users\social> cd \
PS C:\> [System.IO.Path]::GetFullPath("c:foo.txt")
C:\Users\social\foo.txt
PS C:\>

What we see here is that when we change the current directory in PowerShell, it doesn't actually change the current directory .我们在这里看到的是,当我们在 PowerShell 中更改当前目录时,它实际上并没有更改当前目录 That is, PowerShell has its own idea of what the current directory is, but hasn't bothered to tell Windows about the change.也就是说,PowerShell 对当前目录是什么有自己的想法,但没有费心告诉 Windows 有关更改的信息。 You can confirm this with Process Explorer (which can be downloaded from Microsoft's web site);您可以使用 Process Explorer(可以从 Microsoft 网站下载)确认这一点; in the above case, even after using cd , the actual current directory for the PowerShell process was still C:\\Users\\social .在上述情况下,即使在使用cd ,PowerShell 进程的实际当前目录仍然是C:\\Users\\social

You also mention Explorer.您还提到了资源管理器。 As near as I can figure, Explorer does its own validation of the path that it is given, which for whatever reason does not permit drive-relative paths.尽我所能,Explorer 对它给定的路径进行了自己的验证,无论出于何种原因,它都不允许驱动器相关路径。 If the path is not considered valid, or does not point to an actual file/folder, the default action is to open the user's Documents folder.如果认为路径无效,或未指向实际文件/文件夹,则默认操作是打开用户的 Documents 文件夹。

It uses the current working directory on that drive.它使用该驱动器上的当前工作目录。 Each process "remembers" a current working directory per drive:每个进程“记住”每个驱动器的当前工作目录:

 C:\> cd somepath\subdir
 C:\somepath\subdir>  d:
 D:\> dir c:subsubdir       <--  refers to C:\somepath\subdir\subsubdir

Here is the documentation/explanation, courtesy of Harry Johnston's comment .这是文档/解释,由 Harry Johnston 的评论提供

MSDN --> Windows desktop applications --> Develop --> Desktop technologies --> Data Access and Storage --> Local File Systems --> File Management --> About File Management --> Creating, Deleting, and Maintaining Files --> Naming Files, Paths, and Namespaces --> Fully Qualified vs. Relative Paths MSDN --> Windows 桌面应用程序-->开发-->桌面技术-->数据访问和存储-->本地文件系统-->文件管理-->关于文件管理-->创建、删除和维护文件-->命名文件、路径和命名空间--> 完全限定与相对路径

For Windows API functions that manipulate files, file names can often be relative to the current directory, while some APIs require a fully qualified path.对于操作文件的 Windows API 函数,文件名通常可以相对于当前目录,而某些 API 需要完全限定的路径。 A file name is relative to the current directory if it does not begin with one of the following:如果文件名不以以下之一开头,则文件名是相对于当前目录的:

  • A UNC name of any format, which always start with two backslash characters ("\\").任何格式的 UNC 名称,始终以两个反斜杠字符 ("\\") 开头。 For more information, see the next section.有关更多信息,请参阅下一节。
  • A disk designator with a backslash, for example "C:\\" or "d:\\".带有反斜杠的磁盘指示符,例如“C:\\”或“d:\\”。
  • A single backslash, for example, "\\directory" or "\\file.txt".单个反斜杠,例如“\\directory”或“\\file.txt”。 This is also referred to as an absolute path.这也称为绝对路径。

If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter.如果文件名仅以磁盘指示符开头而不以冒号后的反斜杠开头,则将其解释为具有指定字母的驱动器上当前目录的相对路径。 Note that the current directory may or may not be the root directory depending on what it was set to during the most recent "change directory" operation on that disk.请注意,当前目录可能是也可能不是根目录,具体取决于它在该磁盘上最近一次“更改目录”操作期间的设置。 Examples of this format are as follows:这种格式的例子如下:

  • "C:tmp.txt" refers to a file named "tmp.txt" in the current directory on drive C. “C:tmp.txt”指的是C盘当前目录下一个名为“tmp.txt”的文件。
  • "C:tempdir\\tmp.txt" refers to a file in a subdirectory to the current directory on drive C. “C:tempdir\\tmp.txt”指的是驱动器 C 上当前目录的子目录中的文件。

[...] [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Windows 设备路径解析为驱动器号 - Resolve Windows device path to drive letter 从 Windows 路径字符串中提取驱动器号 - Extracting the drive letter from a Windows path string 当路径不包含路径或驱动器号时,文件放置在哪里? - Where are files placed when a path does not include a path or drive letter? Powershell Mount Point Automation:重启后会显示驱动器号 - Powershell Mount Point Automation: Drive letter appears after reboot 为Windows 10设置安装指定盘符和路径不带空格 - Set installation for Windows 10 to specify drive letter and path without spaces 模拟后从映射的驱动器号获取 UNC 路径 - Get UNC path from mapped drive-letter after impersonation Windows 驱动器号映射历史记录 - Windows drive letter mapping history 我怎样才能 map \\wsl$\ 类型 unc 路径到 windows 驱动器号? - How can I map a \\wsl$\ type unc path to a windows drive letter? 在 Windows 中,Node.js 的 path.join() 会自动在当前工作目录的驱动器号 (C:\\) 前面加上 - In Windows, Node.js' path.join() prepends the current working directory's drive letter(C:\) automatically 在 Windows CMD 文件中,后跟斜杠的 echo 有什么作用? - What does an echo followed immediately by a slash do in a Windows CMD file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM