C# 调用控制台程序,并获取输出写入文件
2016-04-28 16:13
633 查看
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Diagnostics; 7 8 namespace CSharpIOTest 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 string file = @"I:\computer_info.cfg"; 15 if (File.Exists(file)) 16 { 17 DisplayVersion(); 18 19 FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write); 20 StreamWriter sw = new StreamWriter(fs); 21 sw.Write(RunCmd("systeminfo")); 22 sw.Flush(); 23 sw.Close(); 24 System.Console.WriteLine("Write data successfully.\r\n"); 25 26 FileParse f = new FileParse(file); 27 System.Console.WriteLine(f.FileContent); 28 System.Console.WriteLine("read data successfully.\r\n"); 29 } 30 } 31 32 private static string RunCmd(string cmd) 33 { 34 Process p = new Process(); 35 p.StartInfo.FileName = "cmd.exe"; 36 p.StartInfo.Arguments = "/c" + cmd; 37 p.StartInfo.UseShellExecute = false; 38 p.StartInfo.RedirectStandardError = true; 39 p.StartInfo.RedirectStandardInput = true; 40 p.StartInfo.RedirectStandardOutput = true; 41 p.StartInfo.CreateNoWindow = true; 42 43 p.Start(); 44 p.StandardInput.WriteLine("exit"); 45 46 return p.StandardOutput.ReadToEnd(); 47 } 48 49 private static void DisplayVersion() 50 { 51 System.Console.ForegroundColor = ConsoleColor.Red; 52 System.Console.WriteLine("Cosmos Copyright 2010 Project"); 53 System.Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 54 System.Console.ForegroundColor = ConsoleColor.White; 55 System.Console.Write("test "); 56 System.Console.ForegroundColor = ConsoleColor.Green; 57 System.Console.WriteLine("2013.10.19"); 58 System.Console.ForegroundColor = ConsoleColor.White; 59 System.Console.WriteLine(); 60 } 61 } 62 }
FileParse.cs:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 7 namespace CSharpIOTest 8 { 9 class FileParse 10 { 11 private string filePath;//文件路径 12 private string fileName;//文件名 13 private string fileContent;//文件内容 14 15 public FileParse() 16 { 17 filePath = string.Empty; 18 fileName = string.Empty; 19 fileContent = string.Empty; 20 } 21 22 public FileParse(string file) 23 { 24 filePath = file; 25 } 26 27 public string FilePath 28 { 29 set { filePath = value; } 30 get { return filePath; } 31 } 32 33 public string FileName 34 { 35 set { fileName = value; } 36 get { return fileName; } 37 } 38 39 public string FileContent 40 { 41 set { fileContent = value; } 42 get 43 { 44 if (File.Exists(filePath)) 45 { 46 FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read); 47 StreamReader sr = new StreamReader(fs); 48 return sr.ReadToEnd(); 49 } 50 else 51 { 52 System.Console.WriteLine("file is empty.\r\n"); 53 return string.Empty; 54 } 55 } 56 } 57 } 58 }
相关文章推荐
- C# 正则表达式及常用正则表达式
- c# winform 关于DataGridView的一些操作(很全,绝对够用)
- C# 线程同步
- C# 多线程传参
- C# 委托:把方法组合到一个数组中使用
- C# 多线程---------<一>
- C#中如何申明固定长度的数组
- C#的类成员初始化顺序
- C# Process.WaitForExit()与死锁
- c# bass入门学习
- C#设置当前程序通过IE代理服务器上网
- c# 多层BOM 树形结构
- C# 正则表达式
- C#构造函数里的base和this的区别
- C#事件与委托
- 自学.NET之路-C#编程之基础总结
- C#中dynamic的正确用法
- C#域验证及遍历域帐号
- C# Wiform GDI+ 模拟图层功能
- C#检查远程或本地磁盘使用率