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及以后版本). ...
随机推荐
- python 爬虫抓取心得
quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quo ...
- 【java】简单的事件总线EventBus
public class EventBus { private static Map<String, EventListener> eventListeners = new HashMap ...
- 【JavaScript】允许IE8使用placeholder
var placeholder = function ($element) { var $ = window.jQuery; var version = parseFloat($.browser.ve ...
- label 行距
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:te ...
- (转)关于rdlc报表的数据源
rdlc 报表字符类数据分为文本数据和表数据,区别就在于文本数据只有一个,表数据可以有多行,然而有很多数据只需要一个传入就可以比如打印某个用户的基本信息,很多信息都是唯一的,如果此时报表传入的数据 ...
- Undefined symbols for architecture i386:"_OBJC_CLASS_$_xx", referenced from: 解决方法
多个人共同操作同一个项目或拷贝项目时,经常会出现类似这样的问题: Undefined symbols for architecture i386: "_OBJC_CLASS_$_xx文件名& ...
- Citrix 服务器虚拟化之五 Xenserver配置存储
Citrix 服务器虚拟化之五 Xenserver配置存储 XenServer中定义了一个容器称为存储库(SR)来描述一个特定的存储目标存储虚拟磁盘映像(VDI). VDI是一个的磁盘抽象,包含一个 ...
- C++学习42 输入和输出的概念
我们经常用到的输入和输出,都是以终端为对象的,即从键盘输入数据,运行结果输出到显示器屏幕上.从操作系统的角度看,每一个与主机相连的输入输出设备都被看作一个文件.除了以终端为对象进行输入和输出外,还经常 ...
- POJ题目分类(转)
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- (转)C# SSL-X509使用
X.509 给出的鉴别框架是一种基于公开密钥体制的鉴别业务密钥管理.一个用户有两把密钥:一把是用户的专用密钥(简称为:私钥),另一把是其他用户都可得到和利用的公共密钥(简称为:公钥).该鉴别框架允许用 ...