概述
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
namespace serviceConfig
{
public class CmdProcess
{
#region 导出
public static string exportData(string businessType)
{
if (businessType=="singleProcess")//单进程导出
{
string strCmd = "";
strCmd = "singleProcess.bat";
string strOutPut = ExecuteDosCmd(strCmd, 30000);
return strOutPut;
}
else//多进程导出
{
string strCmd = "";
strCmd = "exportFromWeb " + businessType;
string strOutPut = ExecuteDosCmd(strCmd, 30000);
return strOutPut;
}
}
#endregion
#region 导入
public static string importData(string businessType)
{
string strCmd = "";
strCmd = "import " + businessType; ;
string strOutPut = ExecuteDosCmd(strCmd, 30000);
return strOutPut;
}
#endregion
#region 注册服务
public static string registerService()
{
string strCmd = "";
strCmd = "instsrv exportToolService exportTool ";
string strOutPut = ExecuteDosCmd(strCmd, 30000);
return strOutPut;
}
#endregion
#region 写log
public static void WriteLogToFile(string writeString)
{
string strDic = System.AppDomain.CurrentDomain.BaseDirectory;
IniMen readIdent = new IniMen();
readIdent.LoadFromFile(strDic + "service.ini");
string logFile = readIdent.ReadIdent("configSet", "selectLogFilePath", "");
FileStream fs = null;
System.DateTime currentTime = System.DateTime.Now;
string filePath = logFile + "\CMD_LOG_" + currentTime.ToLongDateString() + ".txt";
try
{
if (false == System.IO.Directory.Exists(logFile))
{
//创建文件夹
System.IO.Directory.CreateDirectory(logFile);
}
if (File.Exists(filePath))
{
fs = new FileStream(filePath, FileMode.Open);
}
else
{
fs = new FileStream(filePath, FileMode.Create);
}
}
catch (Exception e)
{
e.ToString();
}
fs.Seek(0, SeekOrigin.End);
StreamWriter sw = new StreamWriter(fs);
sw.Write("[");
sw.Write(currentTime.ToString());
sw.Write("]");
sw.Write(writeString);
sw.Write("rn");
sw.Flush();
sw.Close();
fs.Close();
}
#endregion
#region 执行DOS命令,等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待,返回DOS命令的输出,如果发生异常,返回空字符串
public static string ExecuteDosCmd(string dosCommand, int milliseconds)
{
//输出字符串
string output = "";
if (dosCommand != null && dosCommand != "")
{
//创建进程对象
Process process = new Process();
//指定执行路径,开机自己启动时不指定路径,cmd异常退出
process.StartInfo.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
//设定需要执行的命令
process.StartInfo.FileName = "cmd.exe";
//不使用系统外壳程序启动
process.StartInfo.UseShellExecute = false;
//不重定向输入
process.StartInfo.RedirectStandardInput = true;
//重定向输出,而不是默认的显示在dos控制台
process.StartInfo.RedirectStandardOutput = true;
//输出错误信息
process.StartInfo.RedirectStandardError = true;
//不创建窗口
process.StartInfo.CreateNoWindow = true;
//设定参数,其中的“/C”表示执行完命令后马上退出
process.StartInfo.Arguments = "/C " + dosCommand;
try
{
//开始进程
if (process.Start())
{
if (milliseconds == 0)
{
//这里无限等待进程结束
process.WaitForExit();
}
else
{
//这里等待进程结束,等待时间为指定的毫秒
process.WaitForExit(milliseconds);
}
//读取进程的输出结果。
output = process.StandardOutput.ReadToEnd();
if (process.ExitCode == 0)
{
return "export success";
}
else
{
//WriteLogToFile("export 命令执行失败 process.ExitCode= "+ process.ExitCode);
return "export Error";
}
}
}
catch (Exception ex)
{
//WriteLogToFile("export 命令执行失败 错误消息:" + ex.Message);
output = ex.Message;
}
finally
{
if (process != null)
{
process.Close();
process.Dispose();
}
}
}
return output;
}
#endregion
public static string ExecuteCmd(string dosCommand, int milliseconds)
{
// 用ProcessStartInfo启动一个exe程序
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = dosCommand; //exe程序的路径
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
//启动调用
using (Process process = Process.Start(start))
{
WriteLogToFile("ExecuteCmd 开始");
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived );
process.BeginOutputReadLine();
//等待退出
process.WaitForExit();
WriteLogToFile("ExecuteCmd 结束");
}
return "export success";
}
//委托接受exe的输出内容
static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (null != e)
{
Console.WriteLine(e.Data);
WriteLogToFile("process_OutputDataReceived " + e.Data);
}
}
}
}
最后
以上就是魁梧白开水为你收集整理的c# 调用 执行 dos 命令的全部内容,希望文章能够帮你解决c# 调用 执行 dos 命令所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复