关闭

运行外部程序

687人阅读 评论(0) 收藏 举报

/// <summary>
/// 运行外部程序
/// </summary>
/// <param name="exeName">程序路径</param>
/// <returns>0:失败,1:成功</returns>
public bool RunIt( string exeName )
{
//声明一个程序信息类
System.Diagnostics.ProcessStartInfo  Info  =  new  System.Diagnostics.ProcessStartInfo();
//设置外部程序名
Info.FileName  = exeName;
//声明一个程序类
try
{
System.Diagnostics.Process  Proc  ;
Proc  =  System.Diagnostics.Process.Start(Info);
return true;
}
catch
{
return false;
}
}


/// <summary>
/// 判断是否运行
/// </summary>
/// <param name="exeName">程序名</param>
/// <returns>0:没运行,1:运行中</returns>
public bool IsRun( string exeName )
{
string isrunning = "0";
Process[] myProcesses = Process.GetProcesses();
foreach(Process myProcess in myProcesses)
{

if ( myProcess.ProcessName == exeName )
{
isrunning = "1";
break;
}
}
if ( isrunning == "1" )
{
return true;
}
else
{
return false;
}
}

 


/// <summary>
/// 结束进程
/// </summary>
/// <param name="exeName">进程名</param>
/// <returns>0:失败,1:成功</returns>
public bool Kill( string exeName )
{
string isrunning = "0";
Process[] myProcesses = Process.GetProcesses();
foreach(Process myProcess in myProcesses)
{

if ( myProcess.ProcessName == exeName )
{
try
{
myProcess.Kill();
isrunning = "1";
}
catch
{
isrunning = "0";
}
break;
}
}
if ( isrunning == "1" )
{
return true;
}
else
{
return false;
}
}

///////////////////////////////
using System;
using System.Diagnostics;


namespace ZZ
{
 /// <summary>
 /// CmdUtility 的摘要说明。
 /// </summary>
 public class CmdUtility
 {
  //private Process process;

  /// <summary>
  /// 构造函数
  /// </summary>
  public CmdUtility()
  {
   //this.process = new Process();
  }
  /// <summary>
  /// 执行单条命令
  /// </summary>
  /// <param name="commandText">命令文本</param>
  /// <returns>命令输出文本</returns>
  public static string ExeCommand(string commandText)
  {
   Process p = new Process();
   p.StartInfo.FileName = "cmd.exe";
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.RedirectStandardInput = true;
   p.StartInfo.RedirectStandardOutput = true;
   p.StartInfo.RedirectStandardError = true;
   p.StartInfo.CreateNoWindow = true;
   string strOutput = null;
   try
   {
    p.Start();
    p.StandardInput.WriteLine(commandText);
    p.StandardInput.WriteLine("exit");
    //string formats = s.Replace("/r","").Replace("/n","/r/n");
    strOutput = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    p.Close();
   }
   catch(Exception e)
   {
    strOutput = e.Message;
   }
   return strOutput;
  }
  /// <summary>
  /// 执行多条命令
  /// </summary>
  /// <param name="commandArray">命令文本数组</param>
  /// <returns>命令输出文本</returns>
  public static string ExeCommand(string [] commandTexts)
  {
   Process p = new Process();
   p.StartInfo.FileName = "cmd.exe";
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.RedirectStandardInput = true;
   p.StartInfo.RedirectStandardOutput = true;
   p.StartInfo.RedirectStandardError = true;
   p.StartInfo.CreateNoWindow = true;
   string strOutput = null;
   try
   {
    p.Start();
    foreach(string item in commandTexts)
    {
     p.StandardInput.WriteLine(item);
    }
    p.StandardInput.WriteLine("exit");
    strOutput = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    p.Close();
   }
   catch(Exception e)
   {
    strOutput = e.Message;
   }
   return strOutput;
  }
  /// <summary>
  /// 启动外部应用程序
  /// </summary>
  /// <param name="appName">应用程序路径名称</param>
  /// <returns>true表示成功,false表示失败</returns>
  public static bool StartApp(string appName)
  {
   bool blnRst = false;
   Process p = new Process();
   p.StartInfo.FileName = appName;//exe,bat and so on
   p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
   try
   {
    p.Start();
    p.WaitForExit();
    p.Close();
    //while (!p.HasExited)
    //{
    // Thread.Sleep(1000);
    //}
    blnRst = true;
   }
   catch
   {
   }
   return blnRst;
  }
  /// <summary>
  /// 启动外部应用程序
  /// </summary>
  /// <param name="appName">应用程序路径名称</param>
  /// <param name="style">进程窗口模式</param>
  /// <returns>true表示成功,false表示失败</returns>
  public static bool StartApp(string appName,ProcessWindowStyle style)
  {
   bool blnRst = false;
   Process p = new Process();
   p.StartInfo.FileName = appName;//exe,bat and so on
   p.StartInfo.WindowStyle = style;
   try
   {
    p.Start();
    p.WaitForExit();
    p.Close();
    blnRst = true;
   }
   catch
   {
   }
   return blnRst;
  }
  /// <summary>
  /// 启动外部应用程序
  /// </summary>
  /// <param name="appName">应用程序路径名称</param>
  /// <param name="arguments">启动参数</param>
  /// <returns>true表示成功,false表示失败</returns>
  public static bool StartApp(string appName,string arguments)
  {
   bool blnRst = false;
   Process p = new Process();
   p.StartInfo.FileName = appName;//exe,bat and so on
   p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
   p.StartInfo.Arguments = arguments;
   try
   {
    p.Start();
    p.WaitForExit();
    p.Close();
    blnRst = true;
   }
   catch
   {
   }
   return blnRst;
  }
  /// <summary>
  /// 启动外部应用程序
  /// </summary>
  /// <param name="appName">应用程序路径名称</param>
  /// <param name="arguments">启动参数</param>
  /// <param name="style">进程窗口模式</param>
  /// <returns>true表示成功,false表示失败</returns>
  public static bool StartApp(string appName,string arguments,ProcessWindowStyle style)
  {
   bool blnRst = false;
   Process p = new Process();
   p.StartInfo.FileName = appName;//exe,bat and so on
   p.StartInfo.WindowStyle = style;
   p.StartInfo.Arguments = arguments;
   try
   {
    p.Start();
    p.WaitForExit();
    p.Close();
    blnRst = true;
   }
   catch
   {
   }
   return blnRst;
  }
         }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:138796次
    • 积分:1922
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:56篇
    • 译文:2篇
    • 评论:12条
    文章分类