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 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法