首页 > 解决方案 > 如何隐藏由于此过程而创建的 CMD 窗口?

问题描述

我正在尝试运行一个可执行文件作为我的程序的一部分,该文件将在事件处理程序中重复调用。目前,被注释掉的第一行和最后一行按应有的方式运行可执行文件,我遇到的问题是尝试使它,以便用户可以选择在弹出时隐藏该可执行文件(使用收音机完成按钮)

Console.WriteLine("Generated Instruction: " + arguments);                           
             //var proc = System.Diagnostics.Process.Start(chartLocation + @"\MODUS CHaRT CMD.exe", arguments ); // Run Command Line instruction
            Process myProc = new Process();

            if (hideChartStatus)  /* make the process invisible */
            {
                try
                {
                    myProc.StartInfo.CreateNoWindow = true;
                    myProc.StartInfo.UseShellExecute = false;
                    Console.WriteLine("Invisible CHART window generated");
                }
                catch
                {
                    Console.WriteLine("Could not hide CHaRT window");
                }                
            }
            else
            {
                myProc.StartInfo.CreateNoWindow = false;
            }

            myProc.StartInfo.WorkingDirectory = chartLocation;
            myProc.StartInfo.FileName = "\\MODUS CHaRT CMD.exe";
            myProc.StartInfo.Arguments = arguments;
            myProc.Start();

            //myProc = myProc.Start(chartLocation + @"\MODUS CHaRT CMD.exe", arguments);
            //proc.WaitForExit();
            myProc.WaitForExit();

这是我到目前为止所得到的,但我收到错误“System.ComponentModel.Win32Exception:'系统找不到指定的文件'”

在 myProc.start();

我猜这与我如何使用文件名和工作目录有关?

任何人都知道正确的语法吗?

标签: c#processsystem.diagnostics

解决方案


大家别理我,我是个白痴。

更准确地基于这里所做的事情: https ://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?view=netframework-4.7.2

正确的用法是:

//myProc.StartInfo.WorkingDirectory = chartLocation;
            myProc.StartInfo.FileName = chartLocation + @"\MODUS CHaRT CMD.exe";
            myProc.StartInfo.Arguments = arguments;
            myProc.Start();

推荐阅读