ya2*_*a23 6 windows powershell explorer
我错误地输入了一条路,而不是c:\foo.txt写了c:foo.txt.我预计它要么失败要么解决c:\foo.txt,但它似乎解决foo.txt了当前用户的主文件夹.
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
Run Code Online (Sandbox Code Playgroud)
从命令行运行explorer.exe并将其中的任何一个结果传递给要打开的C:\ Users\Administrator\Documents.
我没有找到任何相关文档,我完全糊涂了,请解释一下这种行为.
指定具有驱动器号但没有初始反斜杠的路径时,通常将其解释为指定驱动器上当前目录的相对路径.特别是,这是普通的Win32 API文件函数将如何解释它; 因此,大多数将未修改的文件路径传递给Win32文件函数的软件也会以这种方式运行.
在我的机器上,这在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:\>
Run Code Online (Sandbox Code Playgroud)
我们在这里看到的是,当我们在PowerShell中更改当前目录时,它实际上并不会更改当前目录.也就是说,PowerShell对当前目录的含义有自己的想法,但并没有费心去告诉Windows有关这一变化的信息.您可以使用Process Explorer(可以从Microsoft的网站下载)确认这一点; 在上述情况下,即使在使用之后cd,PowerShell进程的实际当前目录仍然存在C:\Users\social.
你还提到了Explorer.尽可能接近,Explorer会对其给出的路径进行自己的验证,无论出于何种原因,它都不允许驱动器相对路径.如果路径不被视为有效,或者未指向实际文件/文件夹,则默认操作是打开用户的Documents文件夹.
这是标准的DOS/Windows行为,并且一直是这样的.打开命令行并查看:
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
Run Code Online (Sandbox Code Playgroud)
驱动器号始终引用该驱动器的当前目录; (领先)反斜杠将您带到顶级目录.
它使用该驱动器上的当前工作目录。每个进程“记住”每个驱动器的当前工作目录:
C:\> cd somepath\subdir
C:\somepath\subdir> d:
D:\> dir c:subsubdir <-- refers to C:\somepath\subdir\subsubdir
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2089 次 |
| 最近记录: |