C# 调用控制台程序,并获取输出写入文件
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# 调用控制台程序,并获取输出写入文件的更多相关文章
- python将命令输出写入文件或临时缓存
python将命令输出写入文件 将文件写入到对应文件,方便后期处理或保存 def write_file(file_path): with open(file=file_path, mode=" ...
- Net Core 控制台程序使用Nlog 输出到log文件
using CoreImportDataApp.Common; using Microsoft.Extensions.Configuration; using Microsoft.Extensions ...
- 在控制台程序中,添加config文件
一.右击类库 → 添加 → 新建项 → 应用程序配置文件(或者选择一个XML文件,然后将名字改成XXX.config),内容如下: <?xml version="1.0" e ...
- 控制台程序的中文输出乱码问题(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中显示出错 ...
- Java实践-远程调用Shell脚本并获取输出信息
1.添加依赖 <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-s ...
- c# 调用CMD命令并获取输出结果
private static string CMDPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + " ...
- C# 调用外部程序,并获取输出和错误信息
1. 同步模式 public void exec(string exePath, string parameters) { System.Diagnostics.ProcessStartInfo ps ...
- C++调用GDAL库读取并输出tif文件,并计算斑块面积输出景观指数:CSD
部分源码选自GDAL库的官方网址:www.gdal.org,其余的代码为笔者自己编写. // readfile.cpp : 定义控制台应用程序的入口点. // /* part of the codes ...
- spring mvc 引入log4日记记录maven工程 slf4j和log4j输出到控制台配合使用log4j不输出到文件
https://blog.csdn.net/qq_27093465/article/details/62928590 使用slf4j的优点: 提供带参数的日志输出方法(SLF4J 1.7及以后版本). ...
随机推荐
- Codeforces Round #373 (Div. 2)A B
Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...
- Standing on Shouder of Giants
Zachary_Fan 如何一步一步用DDD设计一个电商网站(二)-- 项目架构 http://www.cnblogs.com/Zachary-Fan/p/6012454.html HTTP 权威指南 ...
- [物理学与PDEs]第3章 磁流体力学
[物理学与PDEs]第3章第1节 等离子体 [物理学与PDEs]第3章第2节 磁流体力学方程组 2.1 考虑到导电媒质 (等离子体) 的运动对 Maxwell 方程组的修正 [物理学与PDEs]第3章 ...
- DB2 SQL性能调优秘笈
SQL优化技巧 1.去除在谓词列上编写的任何标量函数 2.去除在谓词列上编写的任何数学运算 3.SQL语句的Select部分只写必要的列 4.尽可能不用Distinct 5.尽量将In子查询重写为Ex ...
- Axis2/c 知识点
官网文档: http://axis.apache.org/axis2/c/core/docs/axis2c_manual.html 从文档中可以总结出: 1. Axis2/C是一个用C语言实现的We ...
- ylbtech-Unitity-CS:Generics
ylbtech-Unitity-CS:Generics 1.A,效果图返回顶部 Unsorted List: Raul:35 Alessandro:30 Maria:72 Hiroyuki:108 A ...
- log4j+logback+slf4j+commons-logging的关系与调试
背景 由于现在开源框架日益丰富,好多开源框架使用的日志组件不尽相同.存在着在一个项目中,不同的版本,不同的框架共存.导致日志输出异常混乱.虽然也不至于对系统造成致命伤害,但是明显可以看出,架构 ...
- RESTful Api 身份认证中的安全性设计探讨
REST 是一种软件架构风格.RESTful Api 是基于 HTTP 协议的 Api,是无状态传输.它的核心是将所有的 Api 都理解为一个网络资源.将所有的客户端和服务器的状态转移(动作)封装到 ...
- 真正的轻量级WebService框架——使用JAX-WS(JWS)发布WebService
WebService历来都很受重视,特别是Java阵营,WebService框架和技术层出不穷.知名的XFile(新的如CXF).Axis1.Axis2等. 而Sun公司也不甘落后,从早期的JAX-R ...
- Java基础-布局