简体   繁体   English

如何从C#执行DOS SET命令并在exe关闭后保持变量?

[英]How do I execute a DOS SET command from C# and have the variable persist after the exe closes?

I have a C# console executable started within a DOS command process. 我有一个在DOS命令进程中启动的C#控制台可执行文件。

I need to be able to execute DOS commands from the C# executable (specifically I need to be able to SET variables) and have the variables persist such that the rest of the DOS process can reference them. 我需要能够从C#可执行文件执行DOS命令(具体来说我需要能够设置变量)并使变量保持不变,以便DOS进程的其余部分可以引用它们。

ie: 即:

Start DOS process -> C# executes a SET command to set UserVariable -> DOS process can ECHO %UserVariable% 启动DOS进程 - > C#执行SET命令设置UserVariable - > DOS进程可以ECHO%UserVariable%

Due to performance reasons I cannot write the set command to a dos script. 由于性能原因,我无法将set命令写入dos脚本。 In fact, I cannot have any file I/O at all. 实际上,我根本不能拥有任何文件I / O.

Can anyone help? 有人可以帮忙吗?

SETX persists the environment variables. SETX持久化环境变量。 Check this: http://technet.microsoft.com/es-es/library/cc755104(v=ws.10).aspx 检查一下: http//technet.microsoft.com/es-es/library/cc755104(v = ws.10).aspx

The problem isn't with the fact that you are calling SET from within a C# application. 问题不在于您从C#应用程序中调用SET。 Even if you open a windows prompt and call SET to set a user variable, it will not persist through sessions. 即使您打开Windows提示符并调用SET来设置用户变量,它也不会在会话中持续存在。

Display, set, or remove CMD environment variables. 显示,设置或删除CMD环境变量。 Changes made with SET will remain only for the duration of the current CMD session. 使用SET进行的更改将仅在当前CMD会话期间保留。

Source . 来源

I advise you to set variables directly through .Net, anyway. 无论如何,我建议你直接通过.Net设置变量。 You can use Environment.SetEnvironmentVariable to do this. 您可以使用Environment.SetEnvironmentVariable来执行此操作。

Use the following .NET method instead: 请改用以下.NET方法:

Environment.SetEnvironmentVariable Environment.SetEnvironmentVariable

If you are running a C# app from a dos script and wishing to use variables set in the app afterwards from the script, I don't know how to do that for just the context of that script from within C#, the other answers here show you for the machine itself but I appreciate you need something with a less permanent scope. 如果您正在从dos脚本运行C#应用程序并希望之后在脚本中使用应用程序中设置的变量,我不知道如何在C#中仅针对该脚本的上下文执行此操作,此处的其他答案显示你对机器本身,但我感谢你需要一个不那么永久的范围。

A meta-programming workaround this could be to: 元编程解决方法可能是:

  • Call the C# app from a DOS FOR loop 从DOS FOR循环调用C#应用程序
  • From the C# app, output to the console SET commands 从C#app,输出到控制台SET命令
  • Use the for loop to execute the app output 使用for循环执行app输出

The calling DOS script would look like this: 调用DOS脚本如下所示:

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

The Console output from MyApp.exe would need to be in the form: MyApp.exe的控制台输出需要采用以下格式:

SET UserVariable1=UserValue1
SET UserVariable2=UserValue2

And then each of the output lines would be executed by the calling FOR loop and the variables would then exist in the context of the calling script. 然后每个输出行将由调用FOR循环执行,然后变量将存在于调用脚本的上下文中。

This is by no means an answer, but with the nature of the problem this "compromise" is the path we have chosen for the time being. 这绝不是一个答案,但就问题的性质而言,这种“妥协”是我们暂时选择的道路。

We'll be using a batch script created by the C# application which will have all the simple set commands needed which will then be executed by the CMD process that invoked the C# application. 我们将使用由C#应用程序创建的批处理脚本,该脚本将具有所需的所有简单set命令,然后由调用C#应用程序的CMD进程执行。

Please feel free to add any further ideas and comments as this is not the ideal solution. 请随意添加任何进一步的想法和评论,因为这不是理想的解决方案。

Unfortunately, so far as I can tell, this is the "solution" that is available, through at least Windows 7. I've been experimenting with various ways to set and subsequently use environment variables, and the bottom line of my research is that, if you want to use a variable, it must either be set in advance into the USER or MACHINE key, or it must be a local variable, set by script command, then tested further along in the same script. 不幸的是,据我所知,这是至少通过Windows 7提供的“解决方案”。我一直在尝试各种方法来设置和随后使用环境变量,我的研究的底线是,如果要使用变量,必须事先将其设置为USER或MACHINE键,或者必须是由script命令设置的局部变量,然后在同一脚本中进一步测试。

So far as I am concerned, the third element in that enumeration, Process, is essentially useless. 就我而言,该枚举中的第三个元素,过程,基本上是无用的。 The only way that I can see that it might be useful is if a process spawns another process, passing its environment to the child process. 我可以看到它可能有用的唯一方法是,进程生成另一个进程,将其环境传递给子进程。 That's overkill for most run-of-the-mill administrative scripts. 对于大多数普通的管理脚本来说,这太过分了。

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

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