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.csdn.net/zzzzzzzert/article/details/8850472


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM