【请问 如何在C#程序中执行 cmd 命令】

清风道禅 2007-11-01 10:21:47
想做一个小 程序··想执行CMD命令·在windows xp环境下,请给点例子,谢谢
...全文
391 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
绝代坏坏 2007-11-01
  • 打赏
  • 举报
回复
楼上正解
王集鹄 2007-11-01
  • 打赏
  • 举报
回复
如果不需要返回结果可以这样
using System.Diagnostics;

Process.Start("cmd.exe", @"/c dir d: > c:\temp\temp.dat");
jeremyyang824 2007-11-01
  • 打赏
  • 举报
回复

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;
}
}
}

zgke 2007-11-01
  • 打赏
  • 举报
回复

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "CMD.EXE";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit();
jeremyyang824 2007-11-01
  • 打赏
  • 举报
回复

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;
p.Start();
string strOutput=null;
// p.StandardInput.WriteLine("cd D:\\flv\\mplayer");
// p.StandardInput.WriteLine("cd d:");
p.StandardInput.WriteLine(string.Format("D:\\flv\\mplayer\\mencoder \"c:\\vs.wmv\" -o \"c:\\output.flv\" -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate={0}:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1 -vf scale=512:-3 -ofps 12 -srate 22050",200));

p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
Console.WriteLine(strOutput);
p.WaitForExit();
p.Close();

清风道禅 2007-11-01
  • 打赏
  • 举报
回复
谢谢了哈`结贴``
微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 微信小程序系统教程共有“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[级阶段]——核心技术”、“微信小程序系统教程[阶段]客服消息+微信支付+九宝电商系统”。 “微信小程序系统教程[阶段]全套课程”包含: 1.微信小程序系统教程[阶段]_客服消息 2.微信小程序系统教程[阶段]_微信支付 3.微信小程序系统教程[阶段]_九宝电商系统 学习“微信小程序系统教程[阶段]”要求有微信小程序的基础。建议先学习“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[级阶段]”,后在报名“微信小程序系统教程[阶段]”。 阶段讲解的客服消息,是针对小程序的。后台程序用接近底层的技术,没有使用三方技术。这样降低同学们学习成本。 微信支付,这部分课程很有难度,要求同学们认真听讲,有不会的技术问题可以请教老师。购买课程后请联系老师,提供单号,给你源程序。 九宝电商系统是一套十分适和学习、项目的课程。既可以系统的学习微信小程序相关知识,还可以修改后上线。 “微信小程序系统教程[级阶段]——核心技术”重点讲解微信小程序事件、组件、API 微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 购买课程的同学,可赠送就九宝老师编写的《微信小程序开发宝典》。 购课请咨询qq2326321088

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧