2015年10月15日 星期四

C# winForm --- Aform 開起其他程式[ Bform.exe ]並傳送參數;

Aform 開起其他程式[ Bform.exe ]並傳送參數;

1. 被開啟的程式[ Bform.exe ]
    Program.cs  內容須修改為可接受參數;
    static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        static System.Threading.Mutex _mutex;
        [STAThread]
        static void Main(string[] args)
        {
            //是否可以打開新進程
            bool createNew;
            Attribute guid_attr = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute));
            string guid = ((GuidAttribute)guid_attr).Value;
            _mutex = new System.Threading.Mutex(true, guid, out createNew);
            if (false == createNew)
            {
                //發現重複進程
            }
            _mutex.ReleaseMutex();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length == 0)
            {
                Application.Run(new Bform());
            }
            else
            {
                Application.Run(new Bform(args[0].ToString()));
            }
        }
    }
/////////////////////////////////
 
   Bform程式需新增

        public Bform(string P) ///定義字串參數P;
        {
            InitializeComponent();
            if (P != "")     ///判斷有沒有參數;
            {
                label1.Text = P;
            }
            else
            {
                label1.Text = "";
            }
        }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. 執行端程式 [ Aform.exe ] 在按鍵中加入;
       private void button_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Bform.exe", "ABC");
        }

         ////System.Diagnostics.Process.Start("程式.exe", "參數");


範例檔案







沒有留言:

張貼留言