C#中怎么实现调用一个编译好的exe文件在后台运行并把exe文件在黑框界面中运行的过程在窗体的控件中显示?

2025-02-27 15:40:18
推荐回答(1个)
回答1:

我这边有点没太理解你的意思,不过我按着我的理解给你写一个方法,你试试看:

public static string runCmd(string command)
        {
            string msg = "";
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";             //设定需要执行的命令 
            p.StartInfo.UseShellExecute = false;          //不使用系统shell外壳程序启动  
            p.StartInfo.RedirectStandardInput = true;     //重定向输入(一定是true) 
            p.StartInfo.RedirectStandardOutput = true;    //重定向输出  
            p.StartInfo.RedirectStandardError = true;
            //p.StartInfo.Arguments = command;
            p.StartInfo.CreateNoWindow = true;            //不创建窗口 
            try
            {
                if (p.Start())
                {
                    p.StandardInput.WriteLine(command);
                    p.StandardInput.WriteLine("exit");
                    msg = p.StandardOutput.ReadToEnd();
                    p.Close();
                }
                else
                    msg = "DOS线程创建失败";

            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return msg;
        }



然后你把你的exe文件的全路径传进去,就相当于在黑框中运行你的exe程序,不知道是不是你要的效果。