-1

I need analyze my environment variable value(COPYCMD) Batch:

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

SUPERCOPY - is my utility

SUPERCOPY code:

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

This string always returns null. Also I have tried another way:

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

This way also doesn't work

Jim
  • 2,974
  • 2
  • 19
  • 29
Sharky
  • 3
  • 3
  • Are you sure you have the element "COPYCMD" in your environmental variables? – Asaf Savich Nov 20 '16 at 14:57
  • 1
    Possible duplicate of [How do I get and set Environment variables in C#?](http://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c) – Alejandro Nov 20 '16 at 14:58
  • In batch files the space to the left of the equals symbol becomes part of the variable name. Remove the space. – Squashman Nov 20 '16 at 15:01
  • Yes, when i use this cycle:foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) Console.WriteLine(" {0} = {1}", de.Key, de.Value); i see my COPYCMD – Sharky Nov 20 '16 at 15:03

1 Answers1

1

I believe the problem is that your environment variable has a trailing space in its name...

Try:

SET COPYCMD=/-y
Thejaka Maldeniya
  • 1,076
  • 1
  • 10
  • 20