Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using environment variable in a file path

I've got an environment variable set that points to a specific folder (call it MYFOLDER for example). When typing in %MYFOLDER%\SubFolder into windows explorer the subfolder appears. However, when I pass SelectedPath = @"%MYFOLDER%\SubFolder"; to a FolderBrowserDialog, it doesn't work.

I tried using Path.GetFullPath(..), but this seems to return the bin folder of the executable (while debugging in VS) with %MYFOLDER% on the end, instead of the path I'd expect.

Anyone know how to get it to use the environment variable properly?

like image 403
Flynn1179 Avatar asked Dec 03 '10 17:12

Flynn1179


People also ask

How do I set Environment Variables in PATH?

To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.

Can we use Environment Variables in properties file?

You can put environment variables in your properties file, but Java will not automatically recognise them as environment variables and therefore will not resolve them. In order to do this you will have to parse the values and resolve any environment variables you find.

Is environment variable a PATH?

PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.


1 Answers

Expand it first:

string path = Environment.ExpandEnvironmentVariables(value); 

http://msdn.microsoft.com/en-us/library/system.environment.expandenvironmentvariables.aspx

like image 156
Marc Gravell Avatar answered Oct 11 '22 10:10

Marc Gravell