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

第一个程序:

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

第二个程序:

需要改Main方法

  1. static class Program
  2. {
  3. /// <summary>
  4. /// 应用程序的主入口点。
  5. /// </summary>
  6. [ STAThread]
  7. public static void Main(string []args) //加参数,接收值
  8. {
  9. Application.EnableVisualStyles();
  10. Application.SetCompatibleTextRenderingDefault( false);
  11. if (args.Length == 0)
  12. {
  13. Application.Run( new Form1());
  14. }
  15. else
  16. {
  17. Application.Run( new Form1(args));
  18. }
  19. }
  20. }

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

  1. string[] args= null;
  2. public Form1(string[] args)
  3. {
  4. InitializeComponent();
  5. //this.ShowIcon = false;
  6. this.ShowInTaskbar = false; //隐藏在系统任务栏
  7. this.WindowState = FormWindowState.Minimized;
  8. //this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
  9. this.args = args;
  10. }

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

猜你喜欢

转载自blog.csdn.net/andrewniu/article/details/80939145
今日推荐