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"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   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 @   fulllove  阅读(2401)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示