2016-11-20 197 views
-1

我需要分析一下我的環境變量的值(COPYCMD) 批次:獲取環境變量

SET COPYCMD = /-y 
SUPERCOPY.exe someFile.some someDirectory 

SUPERCOPY - 是我的工具

SUPERCOPY代碼:

... 
string environmentVar = Environment.GetEnvironmentVariable("COPYCMD"); 
... 

此字符串總是返回null。 此外,我曾嘗試另一種方式:

IDictionary environment = Environment.GetEnvironmentVariables(); 
if ((environment["COPYCMD"] as string).ToUpper() == "/-Y") 
{ 
    DoSomething(); 
} 

這種方式也行不通

+0

您確定在環境變量中有元素「COPYCMD」嗎? – AsafSavich

+1

[我如何在C#中獲取和設置環境變量?](http://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c) – Alejandro

+0

在批處理文件中,等號符號左邊的空格成爲變量名稱的一部分。刪除空間。 – Squashman

回答

1

我相信問題是,你的環境變量有其名稱中的尾隨空格...

嘗試:

SET COPYCMD=/-y