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

后端 未结 4 1376
悲&欢浪女
悲&欢浪女 2021-02-08 20:34

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

4条回答
  •  粉色の甜心
    2021-02-08 21:19

    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.

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

    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.

    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.

提交回复
热议问题