C# 启动exe并传参

方法一:入参格式为(exe路径+空格+传参),例如:string exepath="E:\\呼叫端\\bin\\ces\\Debug\\呼叫器.exe 123"; 

若想在谷歌浏览器上打开某一个exe传参为:C:\Users\Administrator\Chrome\Application\chrome.exe  E:\\呼叫端\\bin\\ces\\Debug\\呼叫器.exe",

若想在浏览器上执行exe并在浏览器上不显示地址内容,则入参需加--app=传,例如:C:\Users\Administrator\Chrome\Application\chrome.exe  --app=E:\\呼叫端\\bin\\ces\\Debug\\呼叫器.exe"

 public bool RunCmd3(string exepath)
        {
            string fullStr = string.Empty;
            bool result = false;
            try
            {
                using (Process myPro = new Process())
                {
                    myPro.StartInfo.FileName = "cmd.exe";
                    myPro.StartInfo.UseShellExecute = false;
                    myPro.StartInfo.RedirectStandardInput = true;
                    myPro.StartInfo.RedirectStandardOutput = true;
                    myPro.StartInfo.RedirectStandardError = true;
                    myPro.StartInfo.CreateNoWindow = true;
                    myPro.Start();
                    //如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
                    fullStr = string.Format("{0}{1}", exepath,"&exit");

                    myPro.StandardInput.WriteLine(fullStr);
                    myPro.StandardInput.AutoFlush = true;
                    myPro.WaitForExit();

                    result = true;
                }

                Utils.LogHelper.WriteLog(GetType(), "Cmd命令执行:" + fullStr);
            }
            catch (Exception ex)
            {
                Utils.LogHelper.WriteLog(GetType(), ex.Message);
            }
            return result;
        }

  方法二:exepath:传参内容;chromePath:启动的exe地址  例如:  string exepath="123";string chromePath="E:\\呼叫端\\bin\\ces\\Debug\\呼叫器.exe";

若想在谷歌浏览器上打开某一个exe传参为: string exepath="123";string chromePath="E:\\呼叫端\\bin\\ces\\Debug\\呼叫器.exe"

若想在浏览器上执行exe并在浏览器上不显示地址内容,则入参需加--app=传,例如: string exepath="123";string chromePath="--app=E:\\呼叫端\\bin\\ces\\Debug\\呼叫器.exe"

   public bool RunCmd3(string exepath,string chromePath)
        {
        try
            {
                using (Process myPro = new Process())
                {
                    myPro.StartInfo.FileName = chromePath;//需要启动的exe路径
                    myPro.StartInfo.Arguments = exepath; //参数传递
                    myPro.StartInfo.UseShellExecute = false;
                    myPro.StartInfo.RedirectStandardInput = true;
                    myPro.StartInfo.RedirectStandardOutput = true;
                    myPro.StartInfo.RedirectStandardError = true;
                    myPro.StartInfo.CreateNoWindow = true;
                    myPro.Start();

                    // 等待浏览器退出
                   // myPro.WaitForExit();

                    result = true;
                }

                Utils.LogHelper.WriteLog(GetType(), "传参为: " + exepath);
            }
            catch (Exception ex)
            {
                Utils.LogHelper.WriteLog(GetType(), "RunChrome异常错误为:" + ex.Message);
            }

            return result;
}

  

posted @ 2023-09-12 11:03  fulllove  阅读(2104)  评论(0编辑  收藏  举报