简体   繁体   中英

OS variables being used as command line arguments

Is it possible to preserve OS variables being used as command line arguments.

For example: Test.exe %Temp%

has the expanded temp variable, not the variable itself.

So:

  Console.WriteLine("Args:\n");
  foreach (string cl in args)
  {
    Console.WriteLine(cl);
  }

Outputs something like:

Args:

C:\Temp

What I need is for the variable to remain unexpanded:

Args:

%Temp%

You need to escape the percentages like this: Test.exe ^%Temp^% . With this you should get the desired output.

Did you try:

Test.exe "%Temp%"

That may be enough to prevent the OS from expanding the environment variable.

Either that, or consider if you really need to pass the OS-specific form of the environment variable name. Without knowing any specifics of Test.exe perhaps you could just use:

Test.exe Temp

Then inside Test.exe you can simply retrieve the environment variable by name. I think this would be more in line with convention anyway.

Also (again, without more detail, it's hard to tell) it seems odd to pass in the name of an environment variable at runtime. Is the name of the variable dynamic? Could you put the name of the environment variable into one that's static, so that Test.exe can just retrieve the name from a consistently-named variable like "Test_exe_init"?

set Text_exe_init=Some_Dynamic_Variable_Name
Test.exe

And inside Text.exe it uses

string foo=GetEnvironmentVariable("Test_exe_init");

How do I get and set Environment variables in C#?

Just a thought...

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