• [转载]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();//从而使外部程序运行完之后才接着往下运行。
    .........接下去的代码

  • 相关阅读:
    最好用的html复制插件——Clipboard.js
    最好用的轮播插件——Swiper.js
    媒体查询
    函数防抖和节流
    JQ增删改查localStorage实现搜索历史功能
    vscode如何设置html模板
    js中innerHTML、outerHTML、innerText、outerText的区别
    JS实现一个简单的网页钟表
    Sql Server 2014完全卸载
    照片尺寸大小怎样换算?
  • 原文地址:https://www.cnblogs.com/chorrysky/p/897342.html
Copyright © 2020-2023  润新知