使用API函数shellexec(参数列表..)

解决方案 »

  1.   

    直接用Process类就行了。
    添加命名空间System.Diagnostics,
    代码如下:
    Process ps = new Process();
    ps.StartInfo.FileName = @"E:\WINDOWS\regedit.exe";
    ps.Start();
      

  2.   

    Process.Start("osql","/E /i c:\\restor1.sql");
      

  3.   

    下面是我的一个类里面的其中两个方法,用来执行命令行程序.
    /// <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;
    }
    下面是执行Windows程序。
    /// <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;
    }
      

  4.   

    if (System.IO.File.Exists(Application.StartupPath+@"\update.exe"))
    {
    System.Diagnostics.Process.Start("update.exe");
    Application.Exit();
    }
    else
    {
    MessageBox.Show("升级文件不存在!");
    }
      

  5.   

    rhs(行云流水) ( ) 
    的方法不错
      

  6.   

    我觉得用process最好了
    用StartInfo.FileName接收要启动的.exe文件的名字
    用Start()函数启动!!!