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及以后版本). ...
随机推荐
- bzoj2005 能量采集 gcd 容斥
ans = sigma_x(sigma_y(gcd(x,y) * 2 - 1)),1<=x<=n,1<=y<=m 枚举x,y,O(nmlogn),超时 换个角度,枚举d = g ...
- windows环境下 生成git公钥和私钥
windows环境下 生成公钥和私钥 上传代码到远程仓库的时候需要秘钥进行验证是否本人上传的.打开Git目录下的Git Bash 输入ssh-keygen,回车 可直接不输入路径,使用默认路径(c/U ...
- 在eclipse中下载包含子模块(Submodules)的git项目
先将项目下载下来 , 这时由于是子项目的原因 , 下载的项目中不包含任何子项目 . 这时在eclipse的Git Repositories中 , 选中Submodules , 右键点击update即可 ...
- Python进阶07 函数对象
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象 ...
- C#委托多播、Lambda表达、多线程、任务
class Program { static void Main(string[] args) { Action<double> ops = MathOperations.Mutiply; ...
- Celery 使用简介
转自:http://liuzxc.github.io/blog/celery/ Celery 是一个简单.灵活且可靠的,处理大量消息的分布式系统,它是一个专注于实时处理的任务队列, 同时也支持任务调度 ...
- CentOS 6 安装 Python3.5以及配置Django
http://www.jianshu.com/p/6199b5c26725 文/FiveStrong(简书作者)原文链接:http://www.jianshu.com/p/6199b5c26725著作 ...
- [SQL]当输入表达式得数为一个有效的整数、浮点数、money 或 decimal 类型,那么 ISNUMERIC 返回 1;否则返回 0
ISNUMERIC ( expression ) 当输入表达式得数为一个有效的整数.浮点数.money 或 :否则返回 .返回值为 确保可以将 expression 转换为上述数字类型中的一种. 注意 ...
- 用浏览器打开本地html 直接到首页 的解决方法
方法1:直接把本地文件拖到浏览器就可以打开
- (medium)LeetCode 233.Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...