using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics; namespace CSharpIOTest
{
class Program
{
static void Main(string[] args)
{
string file = @"I:\computer_info.cfg";
if (File.Exists(file))
{
DisplayVersion(); FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write(RunCmd("systeminfo"));
sw.Flush();
sw.Close();
System.Console.WriteLine("Write data successfully.\r\n"); FileParse f = new FileParse(file);
System.Console.WriteLine(f.FileContent);
System.Console.WriteLine("read data successfully.\r\n");
}
} private static string RunCmd(string cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c" + cmd;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true; p.Start();
p.StandardInput.WriteLine("exit"); return p.StandardOutput.ReadToEnd();
} private static void DisplayVersion()
{
System.Console.ForegroundColor = ConsoleColor.Red;
System.Console.WriteLine("Cosmos Copyright 2010 Project");
System.Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.Console.ForegroundColor = ConsoleColor.White;
System.Console.Write("test ");
System.Console.ForegroundColor = ConsoleColor.Green;
System.Console.WriteLine("2013.10.19");
System.Console.ForegroundColor = ConsoleColor.White;
System.Console.WriteLine();
}
}
}

FileParse.cs:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace CSharpIOTest
{
class FileParse
{
private string filePath;//文件路径
private string fileName;//文件名
private string fileContent;//文件内容 public FileParse()
{
filePath = string.Empty;
fileName = string.Empty;
fileContent = string.Empty;
} public FileParse(string file)
{
filePath = file;
} public string FilePath
{
set { filePath = value; }
get { return filePath; }
} public string FileName
{
set { fileName = value; }
get { return fileName; }
} public string FileContent
{
set { fileContent = value; }
get
{
if (File.Exists(filePath))
{
FileStream fs = new FileStream(filePath,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
return sr.ReadToEnd();
}
else
{
System.Console.WriteLine("file is empty.\r\n");
return string.Empty;
}
}
}
}
}

C# 调用控制台程序,并获取输出写入文件的更多相关文章

  1. python将命令输出写入文件或临时缓存

    python将命令输出写入文件 将文件写入到对应文件,方便后期处理或保存 def write_file(file_path): with open(file=file_path, mode=" ...

  2. Net Core 控制台程序使用Nlog 输出到log文件

    using CoreImportDataApp.Common; using Microsoft.Extensions.Configuration; using Microsoft.Extensions ...

  3. 在控制台程序中,添加config文件

    一.右击类库 → 添加 → 新建项 → 应用程序配置文件(或者选择一个XML文件,然后将名字改成XXX.config),内容如下: <?xml version="1.0" e ...

  4. 控制台程序的中文输出乱码问题(export LC_CTYPE=zh_CN.GBK,或者修改/etc/sysconfig/i18n为zh_CN.GBK。使用setlocale(LC_CTYPE, "");会使用默认办法。编译器会将源码做转换成Unicode格式,或者指定gcc的输入文件的编码参数-finput-charset=GBK。Linux下应该用wprintf(L"%ls/n",wstr))

    今天发现用securecrt登陆时,gcc编译出错时会出现乱码,但直接在主机的窗口界面下用Shell编译却没有乱码.查看了一下当时的错误描述,发现它的引号是中文引号,导致在SecureCRT中显示出错 ...

  5. Java实践-远程调用Shell脚本并获取输出信息

    1.添加依赖 <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-s ...

  6. c# 调用CMD命令并获取输出结果

    private static string CMDPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + " ...

  7. C# 调用外部程序,并获取输出和错误信息

    1. 同步模式 public void exec(string exePath, string parameters) { System.Diagnostics.ProcessStartInfo ps ...

  8. C++调用GDAL库读取并输出tif文件,并计算斑块面积输出景观指数:CSD

    部分源码选自GDAL库的官方网址:www.gdal.org,其余的代码为笔者自己编写. // readfile.cpp : 定义控制台应用程序的入口点. // /* part of the codes ...

  9. spring mvc 引入log4日记记录maven工程 slf4j和log4j输出到控制台配合使用log4j不输出到文件

    https://blog.csdn.net/qq_27093465/article/details/62928590 使用slf4j的优点: 提供带参数的日志输出方法(SLF4J 1.7及以后版本). ...

随机推荐

  1. Java多线程之后台线程

    将线程设置成后台线程Daemons 主线程结果后,后台线程将自动结果. package wzh.test; import java.util.concurrent.TimeUnit; class Si ...

  2. ruby 使用Struct场景

    替代类使用,节省代码,清晰简洁 使用Struct SelectOption = Struct.new(:display, :value) do def to_ary [display, value] ...

  3. 无法向会话状态服务器发出会话状态请求请。确保 ASP.NET State Service (ASP.NET 状态服务)已启动

    原文链接:http://www.cnblogs.com/IT-Bear/archive/2012/01/04/2311546.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET S ...

  4. dedecms v5.7 sp1 给栏目添加缩略图功能

    一.给数据库添加字段typeimg 如图:   二 . 修改 dede/catalog_add.php 找到 $in_query = "INSERT INTO `#@__arctype`(r ...

  5. springmvc笔记(来自慕课网)

    1.准备工作:springmvc相关的jar包. 2.这里我们先用eclipse来操作. 首先看一个接口编程,后面的所有知识点都是通过这个接口编程引出的. OneInterface.java pack ...

  6. 阻止Application_End事件的解决方案

    在做项目时,遇到同步ERP数据的问题,客户要求是:程序中,设置一个开始时间,再设置一个时间间隔,让程序每隔一段时间导出销售记录,这个开始时间和时间间隔可以手动修改设定.     这问题纠缠了我好几天, ...

  7. [SQL]SQL语言入门级教材_SQL功能与特性(一)

    SQL功能与特性 其实,在前面的文章中,已经提及SQL命令的一些基本功能.然而,通过 SQL命令,程序设计师或数据库管理员(DBA)可以: (一)建立数据库的表格.(包括设置表格所可以使用之空间) ( ...

  8. linux spi驱动开发学习-----spidev.c和spi test app

    一.spidev.c文件 看一个设备驱动的方法: module_init标识的入口初始化函数spidev_init,(module_exit标识的出口函数) 设备与设备驱动匹配时候调用的probe方法 ...

  9. Kindle3与亚马逊

    喜欢上亚马逊,偶尔会买些免费或极低价格的书,但始终无法把这些书传到“我的”kindle3上,原因是kindle3无法在中国注册,又绕不开DRM,同时经历了换屏.换主板,早已不是原来的kindle了.今 ...

  10. OC基础(22)

    NSMutableString基本概念 NSMutableString常用方法 NSMutableString练习 *:first-child { margin-top: 0 !important; ...