国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#執行外部命令示例分享

2020-01-24 02:58:29
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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;
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤冈县| 从江县| 定州市| 舒城县| 望都县| 吐鲁番市| 巢湖市| 勃利县| 武宁县| 堆龙德庆县| 翁源县| 独山县| 衢州市| 肥城市| 双流县| 日喀则市| 维西| 深圳市| 富锦市| 资阳市| 河南省| 忻州市| 南江县| 丰镇市| 攀枝花市| 新建县| 冕宁县| 阳朔县| 天祝| 花垣县| 赤城县| 清徐县| 锦州市| 巴彦淖尔市| 彭山县| 龙川县| 关岭| 古田县| 上犹县| 桐柏县| 尼玛县|