Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables in C#

Tags:

c#

I was using an environment variable which was named ABC and had a value of C:/ABC.
In my code I used @"%ABC%/file.txt" for the file path where I had created a folder on the C drive called ABC containing a file called file.txt.

However this does not recognise the environment variable.

Is there any way to make this short-cut work or do I need to manually read the System Environment variable into a separate Environment variable using Environment.GetEnvironmentVariable Method (String) within Visual Studio?

like image 530
Paddy Avatar asked Jan 12 '23 14:01

Paddy


1 Answers

You can do this:

string path = Environment.ExpandEnvironmentVariables(@"%ABC%/file.txt");

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

like image 99
Kamil Budziewski Avatar answered Jan 22 '23 04:01

Kamil Budziewski