c#執(zhí)行外部命令示例分享
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;
}
}
}
相關(guān)文章
C#固定大小緩沖區(qū)及使用指針復制數(shù)據(jù)詳解
這篇文章主要為大家介紹了C#固定大小緩沖區(qū)及使用指針復制數(shù)據(jù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12C#調(diào)用百度翻譯實現(xiàn)翻譯HALCON的示例
HALCON示例程序的描述部分一直是英文的,看起來很不方便。本文就使用百度翻譯實現(xiàn)翻譯HALCON,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET
這篇文章主要用asp.net技術(shù)實現(xiàn)直接在線預(yù)覽word、excel、txt文件,有需要的朋友可以參考下2015-08-08C#實現(xiàn)可捕獲幾乎所有鍵盤鼠標事件的鉤子類完整實例
這篇文章主要介紹了C#實現(xiàn)可捕獲幾乎所有鍵盤鼠標事件的鉤子類,以完整實例形式分析了C#捕獲鍵盤鼠標事件的鉤子操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06