Windows路径,驱动器号和冒号后没有斜杠-它指向什么? [英] Windows path with no slash after drive letter and colon - what does it point to?

查看:145
本文介绍了Windows路径,驱动器号和冒号后没有斜杠-它指向什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入了错误的路径,而不是写了c:foo.txt而不是c:\foo.txt.我希望它会失败或解析为c:\foo.txt,但是相反,它似乎已解析为当前用户的主文件夹中的foo.txt.

I have mistyped a path and instead of c:\foo.txt wrote 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.

Powershell返回:

Powershell returns:

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

从命令行运行explorer.exe并将其传递给上面的任何结果,将在C:\ Users \ Administrator \ Documents中打开.

Running explorer.exe from commandline and passing it any of the above results in C:\Users\Administrator\Documents to be opened.

我没有找到任何相关文档,对此我非常困惑,请解释一下行为.

I haven't found any documentation of that and I'm utterly confused, please explain the behaviour.

推荐答案

当指定的路径带有驱动器号但没有初始反斜杠时,通常将其解释为当前目录的相对路径在指定的驱动器上.特别是,这就是普通Win32 API文件函数将如何解释它;因此,大多数将未修改的文件路径传递给Win32文件功能的软件也将具有这种行为.

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; therefore, most software which passes unmodified file paths to Win32 file functions will also behave this way.

在我的计算机上,这可以在PowerShell中按预期方式工作,但有一个复杂之处:

On my machine, this works as expected in PowerShell, except for one complication:

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:\>

在这里看到的是,当我们在PowerShell中更改当前目录时,它实际上并没有更改当前目录.也就是说,PowerShell对当前目录是什么有自己的想法,但是并没有费心告诉Windows有关更改的信息.您可以使用Process Explorer(可以从Microsoft网站下载)进行确认.在上述情况下,即使使用cd之后,PowerShell进程的 actual 当前目录仍然是C:\Users\social.

What we see here is that when we change the current directory in PowerShell, it doesn't actually change the current directory. That is, PowerShell has its own idea of what the current directory is, but hasn't bothered to tell Windows about the change. You can confirm this with Process Explorer (which can be downloaded from Microsoft's web site); in the above case, even after using cd, the actual current directory for the PowerShell process was still C:\Users\social.

您还提到了资源管理器.据我所知,Explorer会对给定的路径进行自己的验证,无论出于何种原因,Explorer都不允许使用相对于驱动器的路径.如果该路径无效或未指向实际文件/文件夹,则默认操作是打开用户的Documents文件夹.

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. 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.

这篇关于Windows路径,驱动器号和冒号后没有斜杠-它指向什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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