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 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
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.
This is standard DOS/Windows behavior and has always been like this. 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.
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.
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
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. 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 ("\"). For more information, see the next section.
- A disk designator with a backslash, for example "C:\" or "d:\".
- A single backslash, for example, "\directory" or "\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:tempdir\tmp.txt" refers to a file in a subdirectory to the current directory on drive C.
[...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With