C#中执行cmd命令

C#中执行cmd命令,第1张

概述proxy等set、环境变量信息共享 *** 作系统的CMD的配置 注意异常处理、进程的回收 下面实现的缺点是,返回的字符串中有冗余数据,如打开CMD时的运行环境信息,因此可能要自己再去过滤出来哪些才是命令返回的数据 不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态 /// <summary> /// /// </summary> pub proxy等set、环境变量信息共享 *** 作系统的CMD的配置 注意异常处理、进程的回收 下面实现的缺点是,返回的字符串中有冗余数据,如打开CMD时的运行环境信息,因此可能要自己再去过滤出来哪些才是命令返回的数据 不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态
/// <summary>    ///     /// </summary>    public class Cmd    {        /// <summary>        ///         /// </summary>        /// <param name="cmd"></param>        public static string runcmd(string cmd)        {            using (Process proc = new Process())            {                proc.StartInfo.CreateNowindow = true;                proc.StartInfo.filename = "cmd.exe";                proc.StartInfo.UseShellExecute = false;                proc.StartInfo.RedirectStandardError = true;                proc.StartInfo.RedirectStandardinput = true;                proc.StartInfo.RedirectStandardOutput = true;                proc.Start();                proc.Standardinput.Writeline(cmd);                proc.Standardinput.Writeline("exit");                string outStr = proc.StandardOutput.ReadToEnd();                proc.Close();                return outStr;            }        }        /// <summary>        ///         /// </summary>        /// <param name="programname"></param>        /// <param name="cmd"></param>        public static voID RunProgram(string programname,string cmd)        {            using (Process proc = new Process())            {                proc.StartInfo.CreateNowindow = true;                proc.StartInfo.filename = programname;                proc.StartInfo.UseShellExecute = false;                proc.StartInfo.RedirectStandardError = true;                proc.StartInfo.RedirectStandardinput = true;                proc.StartInfo.RedirectStandardOutput = true;                proc.Start();                if (cmd.Length != 0)                {                    proc.Standardinput.Writeline(cmd);                }                proc.Close();            }        }        /// <summary>        ///         /// </summary>        /// <param name="programname"></param>        public static voID RunProgram(string programname)        {            RunProgram(programname,"");        }    }
总结

以上是内存溢出为你收集整理的C#中执行cmd命令全部内容,希望文章能够帮你解决C#中执行cmd命令所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1214784.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存