kevin55

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 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  阅读(5944)  评论(1编辑  收藏  举报