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

时间:2023-10-18 15:30: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;
        }
    }
}

标签:执行外部命令
0
投稿

猜你喜欢

  • 微信跳一跳辅助Java代码实现

    2022-03-31 19:12:03
  • Kotlin入门教程之开发环境搭建

    2022-04-22 20:58:12
  • C#实现给图片添加日期信息的示例详解

    2021-07-29 21:19:28
  • WPF实现自带触控键盘的文本框

    2023-08-25 15:23:20
  • Spring学习教程之AOP模块的概述

    2022-06-04 00:17:13
  • java自定义异常打印内容详解

    2022-03-10 23:58:32
  • Java接口DAO模式代码原理及应用详解

    2023-06-21 05:29:04
  • c语言动态数组示例

    2023-11-02 22:56:44
  • C# 遍历枚举类型的所有元素

    2023-02-06 00:27:46
  • Java中对象的深复制(深克隆)和浅复制(浅克隆)介绍

    2023-02-20 12:05:39
  • 使用spring框架中的组件发送邮件功能说明

    2022-12-29 03:53:55
  • 使用logback实现按自己的需求打印日志到自定义的文件里

    2022-05-12 16:56:49
  • C++中的auto_ptr智能指针的作用及使用方法详解

    2022-04-07 03:01:10
  • Java ArrayList中存放引用数据类型的方式

    2023-11-16 15:23:46
  • Java实现简单学生管理系统

    2023-01-06 16:40:38
  • C++异步操作future和aysnc与function和bind

    2023-06-21 06:48:50
  • C#实现保存文件时重名自动生成新文件的方法

    2022-07-02 15:01:57
  • SpringTask实现定时任务方法讲解

    2022-06-11 11:43:42
  • C++ 线程(串行 并行 同步 异步)详解

    2023-07-18 18:09:43
  • 谈谈变量命名规范的重要性

    2021-08-10 22:40:13
  • asp之家 软件编程 m.aspxhome.com