简体   繁体   English

通过c#从正在运行的应用程序运行命令行

[英]Run a command line from a running application through c#

I've searched around and didn't find anything useful! 我四处搜寻,没有找到任何有用的东西! :( :(

What i want is to have my C# app doing a command to a running process. 我想要的是让我的C#app对正在运行的进程执行命令。 This running process is a console application and i just need to enter the command "restart".. my try was: 这个运行过程是一个控制台应用程序,我只需要输入命令“restart”..我的尝试是:

Process[] processes = Process.GetProcessesByName("OpenSim.32BitLaunch");

foreach (Process p in processes)
{
   p.StandardInput.WriteLine("restart");
}

你应该写p.StandardInput

Instead of 代替

Console.WriteLine("restart");

Use 使用

p.StandardInput.WriteLine("restart");

Thanks by your help guys :) I've managed to solve using SetForegroundWindow and SendKeys. 谢谢你的帮助人:)我设法使用SetForegroundWindow和SendKeys解决。 It was something just like this (remember to import the respective dll's first): 它就像这样(记得首先导入相应的dll):

System.Diagnostics.Process process = Process.GetProcessesByName("OpenSim.32BitLaunch")[0];

        Process[] processes = Process.GetProcessesByName("OpenSim.32BitLaunch");

        foreach (Process p in processes)
          {


              SetForegroundWindow(p.MainWindowHandle);
              Thread.Sleep(1000);
              SendKeys.SendWait("quit");
              Thread.Sleep(1000);
              SendKeys.SendWait("{ENTER}");

          }

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

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