0

My main function takes arguments from command line. This arguments looks like

--args ../../TempImages ../Resources.csproj etc.

How can i find full path to this folder in c# from this arguments? Like

Users/%username%/Projects/Resource/Resource.csproj

I tried use Path, Directory, Enviroment classes, but nothing helps me I am using Xamarin, MacOSX 10.9

EDIT: Forget to say, that i don't know fullpaths as provided above. My script are running on different systems. So, fullpath to these files can be different. Moriarty's answer helps me, but with this condition, it doesn't work.

enc0m
  • 1
  • 2
  • Path.GetFullPath() doesn't wotk correctly? On Windows it works as needed – Alex Voskresenskiy Jul 31 '14 at 06:55
  • https://stackoverflow.com/questions/4796254/relative-path-to-absolute-path-in-c – AndiDog Jul 31 '14 at 06:55
  • 1
    `Path.GetFullPath("../TempImages")` should work. If it doesn't, you should probably post a bug to the Mono devs. – Luaan Jul 31 '14 at 06:55
  • http://stackoverflow.com/questions/867485/c-sharp-getting-the-path-of-appdata?rq=1 have you tried something like this? – sertsedat Jul 31 '14 at 06:56
  • @AlexVoskresenskiy no, it doesn't. He returns me a last directory name `/TempImages` – enc0m Jul 31 '14 at 07:07
  • In regard to the duplicate: this is actually a minor change to the link above; MONO doesn't handle slashes and backslashes well between different environemtns (e.g. "..\"). Hence, the universal anwer as provided below. – Juliën Jul 31 '14 at 08:18

1 Answers1

0

Luckily Mono has adopted this quite well in the System.IO space. You're in the right direction with the Environment classes, like so:

var path = Path.Combine (
    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
    "Projects", 
    "Resource", 
    "Resource.csproj");

The Environment.GetFolderPath, in combination with Environment.SpecialFolder.UserProfile, will do the trick to fetch what is known in Windows as %USERPROFILE%.

Second, notice I've split the /Projects/Resource/ part in seperate arguments for the Path.Combine function. This is to prevent issues with Windows<>Mac<>Linux file operations and let mono figure this out for you.

So this'll work on all platforms. Enjoy!

Update:
For the downvoters that consider this to be a wrong answer: the suggestion mentioned in the referenced duplicate article does not work on Mac or Linux. This is because "/paths/with/slahes" act differently on Mono/Xamarin platform in non-Windows environments. And this is what the original question was about. #imho

Juliën
  • 9,047
  • 7
  • 49
  • 80