场景

Winform中执行cmd命令的工具类,比如调用某些exe,类似mysqldump.exe这样类似的命令。

新建工具类CmdHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace mysqldatabak
{using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;namespace Helper{/// <summary>/// 执行命令/// </summary>public class CmdHelper{////// 执行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();//strOutput = Encoding.UTF8.GetString(Encoding.Default.GetBytes(strOutput));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 onp.StartInfo.WindowStyle = style;p.StartInfo.Arguments = arguments;try{p.Start();p.WaitForExit();p.Close();blnRst = true;}catch{}return blnRst;}}}
}

调用示例

 string cmdStr = mysqlDumpPath + " -h " + this.host.Text.Trim() + " -u" + this.username.Text.Trim() + " -p" + this.password.Text.Trim() + " " + this.database.Text.Trim() + " " + tableName + " > " + "\"" + this.textBox_bak_path.Text.Trim() + "\\" + tableName + ".sql\"";CmdHelper.ExeCommand(cmdStr);

Winform中实现执行cmd命令的工具类相关推荐

  1. C++程序中如何执行cmd命令

    C++程序中如何执行cmd命令 system(const char* _Command) system(const char* _Command) 使用system()函数即可,里面的参数就是cmd命 ...

  2. python执行cmd并返回是否成功_python脚本执行CMD命令并返回结果的例子

    最近写脚本的时想要用python直接在脚本中去执行cmd命令,并且将返回值打印出来供下面调用,所以特意查了下,发现主要有一下几种方式来实现,很简单: 就拿执行adb, adb shell, adb d ...

  3. python 调用控制台并获取返回结果_python脚本执行CMD命令并返回结果的例子

    最近写脚本的时想要用python直接在脚本中去执行cmd命令,并且将返回值打印出来供下面调用,所以特意查了下,发现主要有一下几种方式来实现,很简单: 就拿执行adb, adb shell, adb d ...

  4. python脚本执行CMD命令并返回结果

    最近写脚本的时想要用python直接在脚本中去执行cmd命令,并且将返回值打印出来供下面调用,所以特意查了下,发现主要有一下几种方式来实现,很简单: 就拿执行adb,   adb shell, adb ...

  5. java执行cmd命令 cd_Windows的cmd中cd指令切换路径

    Windows的cmd中cd指令无法转换路径怎么办? 1,首先我们看看盘符,我的电脑里有 C D E盘. 2,按下WIN+R键 输入cmd,打开cmd窗口. 3,默认路径为用户文档路径,如果想切换到D ...

  6. java调用exe_Windows系统中Java调用cmd命令及执行exe程序的方法

    Java调用cmd命令,并输出显示信息: package com.anxin.cmd.test; import java.io.BufferedReader; import java.io.Input ...

  7. java -version cmd_java如何运行步骤cmd?Java执行cmd命令方法有哪些?

    写好一个java程序之后,我们的最终目的就是可以正确的运行程序,如果程序运行正确了,那么代码也就没有什么问题了,可是java如何运行步骤cmd?接下来,我们就来给大家讲解一下这方面的内容. 1.首先用 ...

  8. SQL下三种执行CMD命令的方法

    SQL下三种执行CMD命令的方法: 先删除7.18号日志: (1)exec master.dbo.xp_cmdshell 'del C:/winnt/system32/logfiles/W3SVC5/ ...

  9. python中command是什么意思_python中command执行shell命令脚本方法

    在Python中有一个模块commands也很容易做到以上的效果. 看一下三个函数: 1). commands.getstatusoutput(cmd) 用os.popen()执行命令cmd, 然后返 ...

最新文章

  1. Linux中的文件复制:cp和scp
  2. 将SQL for xml path('')中转义的字符正常显示
  3. python学习笔记--easy_install和pip
  4. r shiny内部支持的输入函数
  5. 可迭代协议与for-of循环
  6. 解决$router.go(-1)返回上一层页面不刷新页面问题
  7. spring-boot-starter-swagger 1.3.0.RELEASE:新增对JSR-303的支持和host的配置
  8. visio能做动态原理图_单、双节锂电池2x20W动态升压双声道音频功放组合方案
  9. c语言求一点到多点最短路径长度,C语言迪杰斯特拉实现最短路径算法(14页)-原创力文档...
  10. HDU-2510(深搜)
  11. 基于51单片机定时,数码管显示时间
  12. A woman without arms
  13. 常用背景色RGB数值
  14. FineReport根据查询参数显示和隐藏列
  15. bool 和_Bool的使用
  16. 如何在android运行lua脚本(最简单的讲解,一看就懂)
  17. 制作通过 NuGet 分发的源代码包时,如果目标项目是 WPF 则会出现一些问题(探索篇,含解决方案)
  18. mmclassification
  19. VBA代码学习for循环
  20. 解决swagger几种报错问题

热门文章

  1. IText实现url转pdf, 解决中文字体问题
  2. linux安装配置jdk1.8
  3. java操作字符串的工具类StringUtil
  4. Netty ObjectPool对象池技术原理分析
  5. mysql数据库运行远程用户访问不了_MySQL数据库远程访问权限如何打开(两种方法)...
  6. python函数使用易错点_Python易错例题
  7. keep 虚拟路线修改器_keep儿童版下载-keep儿童模式6.124.0手机版下载
  8. 将html转换为pptx,javascript – 将html表导出到客户端的ppt?
  9. java跨库join方案_集算器协助java处理多样性数据源之跨库关联
  10. centeos 6.7 mysql 5.7.12_学习centeos7系统 · cdwanze的博文