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

代码如下:

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)

相关推荐

  • C#访问命令行的两种方法

    本文实例讲述了C#访问命令行的两种方法.分享给大家供大家参考.具体如下: 方法1: // 参数:A B C using System; public class CommandLine { public static void Main(string[] args) { // Length 属性用于获取数组的长度. // 注意,Length 是只读属性: Console.WriteLine("Number of command line parameters = {0}", args.

  • C#使用dir命令实现文件搜索功能示例

    本文实例讲述了C#使用dir命令实现文件搜索功能.分享给大家供大家参考,具体如下: 以往,我都是使用 System.IO.Directory.GetDirectories() 和 System.IO.Directory.GetFiles() 方法遍历目录搜索文件.但实际的执行效果始终差强人意,在检索多种类型文件方面不够强大,尤其是在检索特殊文件夹或遇到权限不足时会引发程序异常. 这次为朋友写了个检索图片的小程序,在仔细研究了 Process 以及 ProcessStartInfo 之后,决定利用

  • C#执行DOS命令的方法

    本文实例讲述了C#执行DOS命令的方法.分享给大家供大家参考.具体实现方法如下: 在c#程序中,有时会用到调用cmd命令完成一些功能,本文介绍的如下方法,可实现c#执行DOS命令,并返回结果的功能. 复制代码 代码如下: //dosCommand Dos命令语句  public string Execute(string dosCommand)  {      return Execute(dosCommand, 10);  }  /// <summary>  /// 执行DOS命令,返回DO

  • c# 可疑文件扫描代码(找到木马)(简)

    复制代码 代码如下: using System; using System.IO; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; using System.Net; namespace TrojanScanning { public partial class Form1 : Form { public Form1() { InitializeComponent(

  • C#搜索文字在文件及文件夹中出现位置的方法

    本文实例讲述了C#搜索文字在文件及文件夹中出现位置的方法.分享给大家供大家参考.具体如下: 在linux中查询文字在文件中出现的位置,或者在一个文件夹中出现的位置,用命令: 复制代码 代码如下: grep -n '需要查询的文字' * 就可以了.今天做了一个C#程序,专门用来找出一个指定字符串在文件中的位置,与一个指定字符串在一个文件夹中所有的出现位置. 一.程序代码 using System; using System.Collections.Generic; using System.Lin

  • C# 设计模式系列教程-命令模式

    1. 概述 将一个请求封装为一个对象(即我们创建的Command对象),从而使你可用不同的请求对客户进行参数化; 对请求排队或记录请求日志,以及支持可撤销的操作. 2. 解决的问题 在软件系统中,行为请求者与行为实现者通常是一种紧耦合的关系,但某些场合,比如需要对行为进行记录.撤销或重做.事务等处理时,这种无法抵御变化的紧耦合的设计就不太合适. 3. 模式中角色 3.1 抽象命令(Command):定义命令的接口,声明执行的方法. 3.2 具体命令(ConcreteCommand):具体命令,实

  • C#执行外部命令的方法

    本文实例讲述了C#执行外部命令的方法.分享给大家供大家参考.具体实现方法如下: ///<summary> ///executes a system command from inside csharp ///</summary> ///<param name="cmd">a dos type command like "isql ..."</param> ///<param name ="millsec

  • C#隐式运行CMD命令(隐藏命令窗口)

    本文实现了C#隐式运行CMD命令的功能.下图是实例程序的主画面.在命令文本框输入DOS命令,点击"Run"按钮,在下面的文本框中输出运行结果. 下面是程序的完整代码.本程序没有使用p.StandardOutput.ReadtoEnd()和p.StandardOutput.ReadLine()方法来获得输出,因为这些方法执行后画面容易卡死.而是通过调用异步方法BeginOutputReadLine来获取输出,并在事件p.OutputDataReceived的事件处理方法中来处理结果. u

  • C#调用CMD命令实例

    有时候有一些DOS命令需要我们在执行程序的时候调用,这需要使用C#提供的相关接口. 代码如下,很简单,相信大家都能看懂,我就不赘述了. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics;//这个是进行dos命令调用 namespace ExecuteCMD { //实

  • C#从命令行读取参数的方法

    本文实例讲述了C#从命令行读取参数的方法.分享给大家供大家参考.具体如下: using System; class MainClass { public static void Main(string[] args) { foreach (string arg in args) Console.WriteLine("Arg: {0}", arg); } } 将上面的代码保存为:readfile.cs,编辑源文件: csc readfile.cs 执行命令: readfile.exe j

随机推荐