【问题标题】:How to Provide parameters to a process如何为进程提供参数
【发布时间】:2019-10-17 08:07:36
【问题描述】:

您好,我正在尝试创建一个用 c# 编写的 Windows 窗体应用程序,它将启动、停止并监视它是否正在运行另一个控制台应用程序。

现在我可以在我的 Windows 应用程序上启动控制台应用程序,但是我如何为其 Console.ReadLine 提供值

这是我的代码:

Windows 应用程序:

 int ProcessIDDaca = 111111111;
        private void btnDacaStart_Click(object sender, EventArgs e)
        {

            try
            {
                using (Process myprocess = new Process())
                {
                    myprocess.StartInfo.UseShellExecute = false;
                    myprocess.StartInfo.FileName = @"C:\Users\nx011116\Documents\MachineMonitor\CopyChimp_Server\bin\Debug\CopyChimpServer.exe";
                    //myprocess.StartInfo.FileName = @"C:\Program Files\Eltima Software\Virtual Serial Port Driver 9.0\vspdconfig.exe";
                    myprocess.StartInfo.CreateNoWindow = true;

                    if (btnDacaStart.Text == "Stop")
                    {
                        Process proc = Process.GetProcessById(ProcessIDDaca);
                        proc.Kill();
                        btnDacaStart.Text = "Start";
                        lblDacaID.Text = "";
                    }
                    else
                    {
                        myprocess.Start();
                        var s = myprocess.Id;
                        ProcessIDDaca = myprocess.Id;
                        lblDacaID.Text = s.ToString();
                        btnDacaStart.Text = "Stop";
                    }

                }
            }

            catch (Exception ex)
            {

            }

        }

控制台应用:

  public static string copychimp_server = "";
  public static int port_number = 0;

  static void Main(string[] args)
        {
            Console.Write("Enter copychimp server: ");
            //Here i need to provide
            copychimp_server = Console.ReadLine();
            Console.Write("Enter port number: ");
            //Here i need to provide
            port_number = Convert.ToInt16(Console.ReadLine());


        }

还有为什么没有显示,但我可以看到它正在我的任务管理器上运行? 像编程初学者一样,我非常感谢您的帮助。 提前谢谢你

【问题讨论】:

    标签: c# .net winforms console-application


    【解决方案1】:

    我已经解决了 我的最新代码:

    int ProcessIDDaca = 111111111;
            private void btnDacaStart_Click(object sender, EventArgs e)
            {
    
                try
                {
                    using (Process myprocess = new Process())
                    {
                        myprocess.StartInfo.UseShellExecute = false;
                        myprocess.StartInfo.FileName = @"C:\Users\nx011116\Documents\MachineMonitor\CopyChimp_Server\bin\Debug\CopyChimpServer.exe";
                        //myprocess.StartInfo.FileName = @"C:\Program Files\Eltima Software\Virtual Serial Port Driver 9.0\vspdconfig.exe";
                        myprocess.StartInfo.CreateNoWindow = true;
    
                        if (btnDacaStart.Text == "Stop")
                        {
                            Process proc = Process.GetProcessById(ProcessIDDaca);
                            proc.Kill();
                            btnDacaStart.Text = "Start";
                            lblDacaID.Text = "";
                        }
                        else
                        {
                            myprocess.StartInfo.Arguments = "test";
                            myprocess.Start();
                            var s = myprocess.Id;
                            ProcessIDDaca = myprocess.Id;
                            lblDacaID.Text = s.ToString();
                            btnDacaStart.Text = "Stop";
                        }
    
                    }
                }
    
                catch (Exception ex)
                {
    
                }
    
            }
    
    public static string copychimp_server = "";
      public static int port_number = 0;
    
      static void Main(string[] args)
            {
                Console.Write("Enter copychimp server: ");
                //Here i need to provide
                copychimp_server = args[0];
                Console.Write("Enter port number: ");
                //Here i need to provide
                port_number = Convert.ToInt16(Console.ReadLine());
    
    
            }
    

    参考链接 How to pass parameters to another process in c#

    【讨论】:

      【解决方案2】:

      为什么将 myprocess.StartInfo.CreateNoWindow 设置为 true?将其设置为 false,然后重试。

      【讨论】:

      • 现在它正在显示,但我如何提供给控制台上的 readline?
      • 现在这个winodw和其他console win一样,你不能输入吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 2020-11-06
      • 2013-02-19
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多