一个按钮可以打开和关闭另一个应用程序 [英] One button that opens and closes another app

查看:184
本文介绍了一个按钮可以打开和关闭另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以帮我创建一个按钮,如果点击它将打开控制台应用程序,如果再次单击相同的按钮,它将关闭控制台应用程序



这里我的代码



  private   void  btnStartNStop_Click( object  sender,EventArgs e)
{
if true
{
流程p = 流程()
{
StartInfo = new ProcessStartInfo()
{
FileName = cmd.exe
CreateNoWindow = true
UseShe llExecute = false
ErrorDialog = false
RedirectStandardInput = true
RedirectStandardOutput = true
RedirectStandardError = true
},
EnableRaisingEvents = true
SynchronizingObject =
};

p.OutputDataReceived + =(s,ea)= > .txtOutput.AppendText(ea.Data);

p.Start();
p.BeginOutputReadLine();
}
}

解决方案

您使用CMD.EXE时最明显的错误。从重定向可以明显看出,您无需在交互模式下运行它。因此,您可以使用它从此过程运行其他一些应用程序。这不仅仅是愚蠢的。相反,您应该只需启动所需的应用程序。不需要CMD.EXE,为什么?这只是一个应用程序,没什么特别的。



现在,基本上你需要的是:

  class  SomeClass {

public SomeClass(){
// ...
someButton.Click + =(sender,eventArgs)= < span class =code-keyword>> {
if (externalProcess == null ){
externalProcess = System.Diagnostics.Process.Start(
fileName,
commandLine);
// 将按钮文本更改为停止应用程序
} else {
// 处理输出这里的数据
externalProcess.Kill();
externalProcess = null ;
// 将按钮文本更改为启动应用程序
}
}; // someButton.Click
} // SomeClass

System.Diagnostics.Process externalProcess;

按钮someButton = // ...
string fileName = someApplication.exe; // 无论是什么
string commandLine = 一些命令行参数 ; // 无论是什么

} // SomeClass



非常典型的 SomeClass 将是您的主窗体或主WPF窗口的类,或者您用于UI的东西。



-SA

Hi can you help me create a button if clicked it will open the console app and if you click the same button again it will close the console app

Here My code

private void btnStartNStop_Click(object sender, EventArgs e)
        {
            if (true)
            {
                Process p = new Process()
                    {
                        StartInfo = new ProcessStartInfo()
                        {
                            FileName = "cmd.exe",
                            CreateNoWindow = true,
                            UseShellExecute = false,
                            ErrorDialog = false,
                            RedirectStandardInput = true,
                            RedirectStandardOutput = true,
                            RedirectStandardError = true,
                        },
                        EnableRaisingEvents = true,
                        SynchronizingObject = this
                    };

                p.OutputDataReceived += (s, ea) => this.txtOutput.AppendText(ea.Data);

                p.Start();
                p.BeginOutputReadLine(); 
            }
        }

解决方案

The most apparent mistake you is using "CMD.EXE". It's obvious from redirection that you don't need to run it in interactive mode. Hence, you use it to run some other application from this process. This would be more than silly. Instead, you should just start the application you need. "CMD.EXE" is not needed, why? This is just one of applications, nothing special about it.

Now, here is what you need basically:

class SomeClass {

    public SomeClass() {
        // ...
        someButton.Click += (sender, eventArgs) => {
            if (externalProcess == null) {
                externalProcess = System.Diagnostics.Process.Start(
                    fileName,
                    commandLine);
                // change button text to, say, "Stop the Application"
            } else {
                // handle your output data here
                externalProcess.Kill();
                externalProcess = null;
                // change button text to, say, "Start the Application"
            }
        }; //someButton.Click
    } //SomeClass

    System.Diagnostics.Process externalProcess;

    Button someButton = //...
    string fileName = "someApplication.exe"; // whatever it is
    string commandLine = ""some command line parameters""; // whatever it is

} //SomeClass


Very typically SomeClass would be the class of your main form or main WPF window, or something you use for UI.

—SA


这篇关于一个按钮可以打开和关闭另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆