c#执行外部命令示例分享
String Command = @"python test.py";
String Output = Execute.run(Command);
Console.WriteLine(Output);
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;//using before change the namespace
namespace test.utility
{
class Execute
{
public static String run(String Command)
{
String Output = null;if (Command != null && !Command.Equals(""))
{
Process process = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "cmd.exe";
//no create the cmd windows
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.UseShellExecute = false;process.StartInfo = processStartInfo;
try
{
process.Start();
process.StandardInput.WriteLine(Command);
process.StandardInput.WriteLine("exit");
process.WaitForExit(30 * 1000);
Output = process.StandardOutput.ReadToEnd();
}
catch (Exception e)
{
process.Close();
return e.ToString();
}
finally
{
process.Close();
}
}return ContextFilter(Output);
}public static String ContextFilter(String Output)
{
Regex regex_end = new Regex("^[^^]*#end");
Match match = regex_end.Match(Output);
Regex regex_begin = new Regex("^[^^]*?#begin\r\n");
String result = regex_begin.Replace(match.Value, "");
Regex regex_tar = new Regex("\r\n#end$");
result = regex_tar.Replace(result,"");
return result;
}
}
}
您可能感兴趣的文章:
- ABAP--编写应用服务器执行外部命令的程序示例
- ABAP--编写应用服务器执行外部命令的程序示例
- C#中利用process类调用外部程序以及执行dos命令
- ABAP--编写应用服务器执行外部命令的程序示例
- ABAP--编写应用服务器执行外部命令的程序示例
- C#中利用process类调用外部程序以及执行dos命令 - ASP.NET
- ABAP--编写应用服务器执行外部命令的程序示例
- C#执行外部命令的方法
- Asp.net 在网页编写C#代码示例-- 一个简单的web MsSql 命令执行环境
- ABAP--编写应用服务器执行外部命令的程序示例
- 使用C#调用外部序或是执行DOS命令
- 使用C#调用外部序或是执行DOS命令
- 使用C#调用外部Ping命令获取网络连接情况
- ASP.NET、C#调用外部可执行exe文件--多种方案
- C#执行外部程序之执行DOS命令和批处理
- 《GOF设计模式》—命令(COMMAND)—Delphi源码示例:在不同的时刻指定、排列和执行请求
- C#执行ADD命令是Process.WaitForExit()出现死锁
- C#调用CMD对话框并执行命令
- Java Process Class : Java 执行外部程序及命令
- PHP 执行系统外部命令 system() exec() passthru()