Runtime: Resolve ~ in non-windows systems in Path.GetFullPath()

Created on 12 Dec 2016  路  2Comments  路  Source: dotnet/runtime

Essentially, this is the problem I'm facing

Path.GetFullPath("~/Documents/Temp");

It searches for the the path "~/Documents/Temp" relative to my project folder; but I actually need is the ~/Documents/Temp from the root not from the project path.

This is because I don't want to store all uploaded files in a folder underneath my project folder but in another folder ("possible in another drive or a Network Path").

How is it possible to do this because I'm not getting the actual path no matter what I tried. Thanks!

area-System.IO os-linux

Most helpful comment

~ is a character recognized to have special meaning by some shells, such that the shell expands ~ to mean the current user's home directory, but it's a valid file name character like any other as far as the file system is concerned, and Path doesn't confer to it any special meaning (as is the case with other language's utilities, like C, Go, etc.). Note that even in a shell such as bash you can run commands like:

cd ~
mkdir "~"
cd "~"

That will change you into your home directory, but then create a directory named ~ in your home directory and cd into that, with the quotes escaping the specialness.

All 2 comments

~ is a character recognized to have special meaning by some shells, such that the shell expands ~ to mean the current user's home directory, but it's a valid file name character like any other as far as the file system is concerned, and Path doesn't confer to it any special meaning (as is the case with other language's utilities, like C, Go, etc.). Note that even in a shell such as bash you can run commands like:

cd ~
mkdir "~"
cd "~"

That will change you into your home directory, but then create a directory named ~ in your home directory and cd into that, with the quotes escaping the specialness.

As Stephen mentioned, this is a shell related thing. You can get the folder from Environment.GetFolderPath(SpecialFolder.UserProfile) and use that to build your paths via Path.Combine().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aggieben picture aggieben  路  3Comments

matty-hall picture matty-hall  路  3Comments

jzabroski picture jzabroski  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

omajid picture omajid  路  3Comments