zoukankan      html  css  js  c++  java
  • [转载]C#如何实现对外部程序的动态调用

    [转载]C#如何实现对外部程序的动态调用

    调用cmd.exe程序和外部程序

    using System;
    using System.Diagnostics;


    namespace ApplyCmd
    {
     ///


     /// CmdUtility 的摘要说明。
     ///
     public class CmdUtility
     {
      
      ///
      /// 执行cmd.exe命令
      ///
      ///
      /// 命令输出文本
      public static string ExeCommand(string commandText)
      {
       return ExeCommand(new string []{commandText});
      }
      ///
      /// 执行多条cmd.exe命令
      ///
      ///
      /// 命令输出文本
      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应用程序,隐藏程序界面
      ///
      ///
      /// true表示成功,false表示失败
      public static bool StartApp(string appName)
      {
       return StartApp(appName,ProcessWindowStyle.Hidden);
      }
      ///
      /// 启动外部应用程序
      ///
      ///
      ///
      /// true表示成功,false表示失败
      public static bool StartApp(string appName,ProcessWindowStyle style)
      {
       return StartApp(appName,null,style);
      }
      ///
      /// 启动外部应用程序,隐藏程序界面
      ///
      ///
      ///
      /// true表示成功,false表示失败
      public static bool StartApp(string appName,string arguments)
      {
       return StartApp(appName,arguments,ProcessWindowStyle.Hidden);
      }
      ///
      /// 启动外部应用程序
      ///
      ///
      ///
      ///
      /// true表示成功,false表示失败
      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;
      }
     }
    }

    ps:利用System.Diagnostics.Process来压缩文件或文件夹

    string strArg = "a -r  {0} {1}";
        System.Diagnostics.Process.Start(@"C:\Program Files\WinRAR\rar.exe", String.Format(strArg, txtApp.Text+".rar", txtApp.Text));

    strArg为winrar的命令参数,请参考帮助。

     
    .NET环境中,在程序里调起其他应用程序

    以程序中调起写字板为例:

    using System.Diagnostics;

    ......

    Process.Start("notepad.exe","c:\test.txt");

    说明:

    用到System.Diagnostics包,

    Process.Start( [ 应用程序可执行文件的路径 ] , [参数] ) ;


    使用System.Diagnostics.Process.Start调用外部程序,如何等待外部程序执行完,再从调用处接着往下执行。

    System.Diagnostics.Process p1= Process.Start("sqlplus.exe", connectstring+" @"+filename);
    p1.WaitForExit();//从而使外部程序运行完之后才接着往下运行。
    .........接下去的代码

  • 相关阅读:
    centos 7 安装VCL播放器
    pheatmap, gplots heatmap.2和ggplot2 geom_tile实现数据聚类和热图plot
    R语言通过loess去除某个变量对数据的影响
    安卓手机免root实现对其他软件最高管理(sandbox思想)
    R语言写2048游戏
    R语言各种假设检验实例整理(常用)
    R语言实现对基因组SNV进行注释
    R语言实现二分查找法
    将基因组数据分类并写出文件,python,awk,R data.table速度PK
    PHP设计模式练习——制作简单的投诉页面
  • 原文地址:https://www.cnblogs.com/chorrysky/p/897342.html
Copyright © 2011-2022 走看看