C# 调用控制台程序,并获取输出写入文件

 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 }

posted on 2013-10-20 22:23 kernel_main 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/kernel0815/p/3379650.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值