How to execute a DOS SET command with C # and save a variable after exe closes?

I have a C # console executable launched as part of a DOS command.

I need to be able to execute DOS commands from the C # executable (in particular, I need to be able to set SET variables) and the variables are saved so that the rest of the DOS process can reference them.

t

Start the DOS process -> C # executes the SET command to set UserVariable -> The DOS process can ECHO% UserVariable%

Due to performance reasons, I cannot write the set command in a dos script. In fact, I generally cannot have file I / O.

Can anyone help?

+4
source share
6 answers

If you use a C # application from a dos script and want to use the variables set in the application after that from a script, I don’t know how to do this only for the context of this script from within C #, the other answers here show you for the machine itself, but I appreciate that you need something with a less constant area.

A workaround for metaprogramming can be as follows:

  • Call C # application from DOS FOR loop
  • In a C # application, output the SET console commands
  • Use the for loop to execute application output

The calling DOS script will look like this:

FOR /F "tokens=* delims=" %%A IN ('MyApp.exe') DO ( %%A ) 

The console output from MyApp.exe should be in the form:

 SET UserVariable1=UserValue1 SET UserVariable2=UserValue2 

And then each of the output lines will be executed by the calling FOR loop, and then the variables will exist in the context of the calling script.

+1
source

SETX saves environment variables. Check this out: http://technet.microsoft.com/es-es/library/cc755104 (v = ws.10) .aspx

+4
source

The problem is not that you are calling SET from a C # application. Even if you open a Windows prompt and call SET to set a user variable, it will not be saved through sessions.

Display, set, or delete CMD environment variables. Changes made with SET will only remain for the duration of the current CMD session.

Source

I advise you to set the variables directly through .Net, anyway. You can use Environment.SetEnvironmentVariable for this.

+3
source

Instead, use the following .NET method:

Environment.SetEnvironmentVariable

+1
source

This is by no means the answer, but with the nature of the problem, this “compromise” is the path that we have chosen at the moment.

We will use a batch script created by a C # application that will have all the necessary simple commands that will then be executed by the CMD process that invokes the C # application.

Feel free to add additional ideas and comments as this is not an ideal solution.

0
source

Unfortunately, as far as I can tell, this is a "solution" that is available, at least in Windows 7. I experimented with various ways of setting and using environment variables, and the bottom line of my research is that if you want to use variable, it must either be predefined in the USER or MACHINE field, or it must be a local variable specified by the script command, and then checked further in the same script.

As far as I know, the third element in this enumeration of the “Process” is essentially useless. The only way I can see that this can be useful is through a process that creates another process that passes its environment to a child process. This is an overflow for most administration scenarios.

-1
source

All Articles