Paddy
Paddy

Reputation: 936

Environment variables in 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?

Upvotes: 4

Views: 509

Answers (5)

Rohit
Rohit

Reputation: 10226

Try

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

Upvotes: 1

It'sNotALie.
It'sNotALie.

Reputation: 22794

Use Environment.ExpandEnvironmentVariables:

string expanded = Enviroment.ExpandEnvironmentVariables(input);

Upvotes: 2

Zaki
Zaki

Reputation: 5600

use System.Environment class.

System.Environment.ExpandEnvironmentVariables("")

Upvotes: 1

Kamil Budziewski
Kamil Budziewski

Reputation: 23087

You can do this:

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

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

Upvotes: 3

xanatos
xanatos

Reputation: 111810

There is an aptly named Environment.ExpandEnvironmentVariables that should do the work for you.

Upvotes: 2

Related Questions