C#启动另一个应用程序并传参数

时间:2023-03-09 07:55:29
C#启动另一个应用程序并传参数

第一个程序:

          try
{ ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WindowsFormsApplication1.exe"; //启动的应用程序名称
startInfo.Arguments = "我是由控制台程序传过来的参数,如果传多个参数用空格隔开"+" 第二个参数";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startInfo); }
catch (Exception ex)
{
throw;
}

第二个程序:

需要改Main方法

    static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main(string []args) //加参数,接收值
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
Application.Run(new Form1());
}
else
{
Application.Run(new Form1(args));
}
}
}

  Form1()窗口增加构造函数:

 string[] args=null;
public Form1(string[] args)
{
InitializeComponent();
//this.ShowIcon = false;
this.ShowInTaskbar = false; //隐藏在系统任务栏
this.WindowState = FormWindowState.Minimized;
//this.FormBorderStyle = FormBorderStyle.SizableToolWindow; this.args = args; }

  如此,利用传过来的参数就可以在第二个程序里执行了

转自:https://blog.****.net/zzzzzzzert/article/details/8850472