手机
当前位置:查字典教程网 >编程开发 >C#教程 >c#执行外部命令示例分享
c#执行外部命令示例分享
摘要:复制代码代码如下:StringCommand=@"pythontest.py";StringOutput=Execute.run(Comma...

复制代码 代码如下:

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("^[^^]*?#beginrn");

String result = regex_begin.Replace(match.Value, "");

Regex regex_tar = new Regex("rn#end$");

result = regex_tar.Replace(result,"");

return result;

}

}

}

【c#执行外部命令示例分享】相关文章:

C#利用子线程刷新主线程分享教程

c# 命名空间和程序集

C#中执行批处理文件(*.bat)的方法代码

c#动态调用Webservice的两种方法实例

ScriptControl控件执行自定义VBS脚本示例分析

c# list部分操作实现代码

C# 执行bat批处理文件的小例子

c#解压文件的实例方法

c#防止多次运行代码收集分享

c#启动EXE文件的方法实例

精品推荐
分类导航