程序调用CMD&输出重定向到指定文件
2012-09-22 15:58
393 查看
题记:在自己的程序中调用CMD执行某个命令,将结果输出到某文件保存,以供使用
两种方法:
1、调用CMD后逐行读取写入文件
2、使用CMD的重定向输出命令
View Code
两种方法:
1、调用CMD后逐行读取写入文件
2、使用CMD的重定向输出命令
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace aboutls { internal class DosCommandDo { public static void Execute(string dosCommand, string fileName, int milliseconds) { if (dosCommand != null && dosCommand != "") { //TODO judgement the fileName Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/c " + dosCommand; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = false; startInfo.RedirectStandardOutput = true; startInfo.CreateNoWindow = true; process.StartInfo = startInfo; try { if (process.Start()) { if (milliseconds == 0) process.WaitForExit(); else process.WaitForExit(milliseconds); System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName, false, System.Text.Encoding.GetEncoding("gb2312")); System.IO.StreamReader reader = process.StandardOutput;//截取输出流 string line = reader.ReadLine();//每次读取一行 sw.WriteLine(line); sw.Flush(); while (!reader.EndOfStream) { line = reader.ReadLine(); sw.WriteLine(line); sw.Flush(); } if (reader != null) reader.Close(); if (sw != null) sw.Close(); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (process != null) process.Close(); } } } public static void ExecuteOut(string dosCommand, string fileName, int milliseconds) { if (dosCommand != null && dosCommand != "") { Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/c " + dosCommand + " > "+fileName; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = false; startInfo.RedirectStandardOutput = true; startInfo.CreateNoWindow = true; process.StartInfo = startInfo; try { if (process.Start()) { if (milliseconds == 0) process.WaitForExit(); else process.WaitForExit(milliseconds); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (process != null) process.Close(); } } } } }
相关文章推荐
- 程序调用CMD&输出重定向到指定文件
- 无法启动程序'...debug/abc.exe',系统找不到指定文件"的问题
- wxWidgets调用指定程序打开文件代码
- 调用Windows关联程序打开指定文件
- 将top命令查看指定程序的结果输出到文件
- [转帖]调用explore打开指定文件所在的文件夹,并把目标文件设为选定状态,甚至.lnk文件也能自动找到它的目标执行程序文件。
- 从 stdin 读取输入的C程序示例 && python脚本调用可执行文件
- cmd chcp命令切换字符格式 cmd重定向输出到文件出现中文乱码的处理方法
- 调试程序时在不中断程序的情况下输出函数调用信息(Mac OS,Linux & Windows)
- cmd 中连接mysql时报'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件,该怎么办?
- 通过C++程序调用cmd并进入指定路径
- 编译结果>重定向到文件,发现error信息不写到文件,还是在终端输出
- nohup 后台运行,以及重定向标准输出和标准错误 &/dev/null 文件
- C# 调用控制台程序,并获取输出写入文件
- 调试程序时在不中断程序的情况下输出函数调用信息(Mac OS,Linux & Windows)
- Linux下Shell输出重定向到空设备文件1>/dev/null 2>&1
- Python第十天 print >> f,和fd.write()的区别 stdout的buffer 标准输入 标准输出 从控制台重定向到文件 标准错误 重定向 输出流和输入流 捕获sys.exit()调用
- command > filename 2>&1 把标准输出和标准错误一起重定向到一个文件中
- (2012.01.03) 输出程序中的指定位置的行数与该文件的路径及标题。
- perl中调用外部程序,要注意把外部程序的输出重定向到/dev/null