C#调用外部执行文件(我是写的winform).exe 并传递一个或多个参数

一、题外话

首先应引入进程的命名空间  using  System.Diagnostics;   

启动进程:Process proexe = Process.Start(@""+this.txtAddress.Text+"");

注:这个是需要得到绝对地址,一般运行时地址都是变的  一般使用的是下面两个

Application.StarupPath 获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

举例:则他获取到得结果就是c:\test\bin\debug

Application.ExecutablePath 获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

举例 程序路径是c:\test\bin\debug\test.exe
 

一、在C#外部调用该exe文件时 习惯把能调用的exe文件可执行文件夹放到当前文件夹内 

在需要调用的地方写入代码(不需要传参)

  Process processexe = Process.Start(Application.StartupPath + "\\exe所在文件夹\\执行文件.exe");

注:转义符写法 \\或者@"\   均可

1.传参写法

 Process proexe = Process.Start(Application.StartupPath + "\\测试.exe"," 11  22 33");

第一个参数是启动.exe 的路径,第二个参数是两个实参数组成的字符串。注意:在第二个中如果传递多个实参 需要在同一  "  " 内参数间用空格区分(一个或多个空格是一样的作用) 不要用逗号,用逗号的话是默认为作为参数传递的 亲测 

2.也可以这样写

string args = "11   22  33";

Process proexe = Process.Start(Application.StartupPath + "\\测试.exe",args);

二、在被调用的执行程序里写法: winform程序内

在Program类里面

 static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //if (args.Length > 0)
            //{
               // MessageBox.Show(":" + args[0] + args[1]);
            //}
            //else { MessageBox.Show("啥:"); }  //这是测试有没有参数传递过来
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(args));
        }
    }
}

在窗体的代码界面下 首先加一个构造函数  

 public Form1(string[] args)
        {
            arg = args;
            InitializeComponent();
        }

然后就是在需要调用的时候 调用就好了

猜你喜欢

转载自blog.csdn.net/feelsyt/article/details/106226327