简体   繁体   English

C#中的环境变量

[英]Environment variables in C#

I was using an environment variable which was named ABC and had a value of C:/ABC. 我使用的是一个名为ABC的环境变量,其值为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. 在我的代码中,我使用@“%ABC%/ file.txt”作为文件路径,我在C驱动器上创建了一个名为ABC的文件夹,其中包含一个名为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? 有没有办法使这个快捷方式工作或我需要使用Visual Studio中的Environment.GetEnvironmentVariable方法(字符串)手动将系统环境变量读取到单独的环境变量中?

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

使用Environment.ExpandEnvironmentVariables

string expanded = Enviroment.ExpandEnvironmentVariables(input);

使用System.Environment class

System.Environment.ExpandEnvironmentVariables("")

尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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