时间:2021-05-20
复制代码 代码如下:
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;
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#执行外部命令的方法。分享给大家供大家参考。具体实现方法如下://////executesasystemcommandfrominsidecsh
不多说,直接上干货!1.impala-shell外部命令所谓的外部命令指的是不需要进入到impala-shell交互命令行当中即可执行的命令参数。impala-
Unix中的dot命令dot命令语法:.外部命令将外部命令放在当前进程(而不是子进程)环境中运行(类似c语言中的#include语句),并继承当前进程(shel
这几天在用golang写一个工具,要执行外部命令工具,而且还要将外部命令工具输出的日志也要输出出来。网上找了一下,资料很多,关键是执行的结果成功或失败状态没找到
Linux的命令分为内部命令和外部命令:1.内部命令在系统启动时就调入内存,是常驻内存的,所以执行效率高。2.外部命令是系统的软件功能,用户需要时才从硬盘中读入