当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 用C#实现启动另一程序的方法

ASP.NET
.Net基础:在ASP.net中网站访问量统计方法
Asp.Net 建立一个在线 RSS 新闻聚合器
ASP.NET从字符串中查找字符出现次数的方法
了解ASP.NET中的IFRAME框架挂马
JAVA和.NET两个平台对于安全功能的比较
.NET中*延迟*特性的几个陷阱
使用ASP.NET Global.asax 文件
在.NET环境下为网站增加IP过滤功能
如何实现.net程序的进程注入
在.Net Micro Framework中显示汉字
引以为戒 .NET开发者常犯的错误
WinForm程序中使用控制台作为输出窗口
浅谈如何使用 Lambda 表达式做抽象代表
.Net基础:C#中对DatagridView部分常用操作
ASP.NET LinkButton组件编程浅析
ASP.NET中使用AJAX中的方式
ASP.NET组件设计之生命周期详解
asp.net下web控件点评
.Net应用:ASP.NET中使用AJAX中的方式
.Net基础:ASP.NET中的javascript操作

ASP.NET 中的 用C#实现启动另一程序的方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 50 ::
收藏到网摘: n/a

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