简体   繁体   中英

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?

You can do this:

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

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

有一个恰当命名的Environment.ExpandEnvironmentVariables应该为你工作。

使用Environment.ExpandEnvironmentVariables

string expanded = Enviroment.ExpandEnvironmentVariables(input);

使用System.Environment class

System.Environment.ExpandEnvironmentVariables("")

尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM