Winform中实现执行cmd命令的工具类

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 Winform中实现执行cmd命令的工具类,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

场景

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 on
                p.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);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/136183.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!