« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
|
统计 |
blog名称:小雨 日志总数:262 评论数量:1273 留言数量:15 访问次数:4677718 建立时间:2005年1月8日 |
| 
|
W3CHINA Blog首页 管理页面 写新日志 退出
[经验杂谈]在C#里,如何执行cmd里的常用dos命令 (thx 花葬) |
小雨 发表于 2005/3/30 21:10:12 | using System;using System.Diagnostics;
namespace Tipo.Tools.Utility{ /// <summary> /// 常用Dos命令操作 /// </summary> public class DosCommand { private Process process=null; private string _errmsg;
public DosCommand() { process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = false; process.Start(); }
//检测文件或目录是否存在 public string exist(string path) { process.StandardInput.WriteLine(string.Format("dir \"{0}\"",path)); process.StandardInput.WriteLine("exit"); _errmsg = process.StandardError.ReadToEnd(); return _errmsg; } }}
|
阅读全文(24333) | 回复(6) | 编辑 | 精华 |
回复:在C#里,如何执行cmd里的常用dos命令 (thx 花葬) |
求助者(游客)发表评论于2008/8/26 16:22:46 | Process process = new Process();
process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput=true; process.StartInfo.RedirectStandardOutput=true; process.StartInfo.RedirectStandardError=true; process.StartInfo.CreateNoWindow=true; process.Start(); process.StandardInput.WriteLine(Application.StartupPath + @"\corvars.bat"); process.StandardInput.WriteLine("exit"); message = process.StandardOutput.ReadToEnd(); process.WaitForExit(30); process.Close();
我在程序执行的文件夹内创建了一个批处理文件, 想用多线程执行这个批处理程序,但还不想看到CMD的窗口 我写了这个 批处理文件能出来 但不能调用 请大家帮我看看
|
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除 |
回复:在C#里,如何执行cmd里的常用dos命令 (thx 花葬) |
赣州冶院(游客)发表评论于2007/11/19 21:18:00 | 如果想在执行完命令后不退出CMD,该怎么办? 把p.StandardInput.WriteLine("exit");这条删除的话是不行的
|
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除 |
回复:在C#里,如何执行cmd里的常用dos命令 (thx 花葬) |
111(游客)发表评论于2007/5/8 15:08:35 |
这个是返回到LABEL框里面
如何可以直接打开CMD进程并同时执行命令? Process.Start("cmd","TELNET 127.0.0.1" );
不能用,还有其他方法吗?
以下为blog主人的回复:
那要自己写了,不过这个有个返回值,你看行不行,我现在没有环境试验了
|
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除 |
回复:在C#里,如何执行cmd里的常用dos命令 (thx 花葬) |
小雨发表评论于2006/6/15 21:11:40 | using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Diagnostics;
namespace WindowsApplication1{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(16, 224); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.Location = new System.Drawing.Point(40, 40); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(208, 176); this.label1.TabIndex = 1; this.label1.Text = "label1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.label1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
private void Form1_Load(object sender, System.EventArgs e) { }
private void button1_Click(object sender, System.EventArgs e) { 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 = false;
string pingrst;
p.Start(); p.StandardInput.WriteLine("dir d:\\");
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.Close();
label1.Text=strRst; } }}
|
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除 |
回复:在C#里,如何执行cmd里的常用dos命令 (thx 花葬) |
Fossil(游客)发表评论于2005/6/24 23:43:04 | 以下引用nrzj在2005-3-30 21:13:08的评论:要取返回值的话,再修改一下最后返回的那个类型就可以了,懒得改啦,这样马马乎乎能用 谢谢,我正急这个问题,很受用!
|
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除 |
回复:在C#里,如何执行cmd里的常用dos命令 (花葬) |
小雨发表评论于2005/3/30 21:13:08 | 要取返回值的话,再修改一下最后返回的那个类型就可以了,懒得改啦,这样马马乎乎能用
|
个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除 |
» 1 »
|