·天新网首页·加入收藏·设为首页
首页|笔记本|手机|数码相机|摄像机|MP3/MP4|主板|内存|显示器|办公|打印机|下载|开发|汽车|学院|业界
硬件|台式机|数码|数字家庭|投影仪|GPS/CPU|显卡|硬盘|服务器|网络|一体机|驱动|源码|游戏|考试|报价
您现在的位置:天新网 > 软件开发 > .Net开发 > C#
用C#实现启动另一程序的方法
http://dev.21tx.com 2007年11月14日 网页教学网

  一段实例代码,程序的目的是使用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 > 0) break;
  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);
  }
  }

上一篇: C#中的两个+(plus)操作符解析
下一篇: C#学习之类的访问修饰符

Google
 
热点文章
关于我们 | 联系我们 | 广告服务 | 工作机会 | 版权声明 | 欢迎投稿 | 网站地图
Copyright © 2000-2008 , www.21tx.com , All Rights Reserved .
© 晨新科技 版权所有 Created by TXSite.net