C#中啓動另一個程序的有效方法

在工作過程中。技術總監給出了這樣一個有效的啓動程序的有效方法,現在和大家分享下:

 

private   void  btnCreate_Click( object  sender, EventArgs e)
        
{
            
int hWnd = FindWindow(null"test");//窗體的名稱
            
//check if PowerReuse is launched or not
            
//if yes, pass path of project to PowerReuse
            
//or, launch PowerReuse with specified parameter
            if (hWnd > 0)
            
{
                MessageBox.Show(
"powerReuse has been launched already." + " " + hWnd.ToString());
                
//SendMessage to PowerReuse
                return;
            }


            
try
            
{
                Process Main_P 
= new Process();
                
//this path should be retrieved from Windows Registry,
                
//the loaction is written by Installter during process of installation.
                Main_P.StartInfo.FileName = @"C: est.exe";//運行的exe路徑
                
                
//This URL is passed to PowerReuse to open
                Main_P.StartInfo.Arguments = @"C:Tempabc.prj";//運行時的參數

                Main_P.StartInfo.UseShellExecute 
= true;
                
                Main_P.Start();
                
//
                
//we have to wait for a while until UI has been initialized
                
//
                Main_P.WaitForInputIdle(10000);

                
//although UI has been initialzied,
                
//it does not mean main form of application has been completed.
                
//we may wait for another 10 seconds
                for (int i = 0; i < 100; i++)
                
{
                    hWnd 
= FindWindow(null"PowerReuse (Beta)");
                    
//hWnd = Main_P.MainWindowHandle.ToInt32() ;
                    if (hWnd > 0break;
                    Thread.Sleep(
100);
                }


                
//Here, we check if PowerReuse is fully launched 
                if (hWnd == 0)
                
{
                    
//Handle exception
                    MessageBox.Show("We cannot find window handle of PowerReuse");
                }

                
else
                
{
                    
//other handling
                    
//
                    MessageBox.Show(hWnd.ToString() + " " + Main_P.MainWindowHandle.ToString() + " " + Main_P.MainWindowTitle);
                }

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值