SoFunction
Updated on 2025-03-07

C# Execution of External Command Example Sharing


using System;
using ;
using ;
using ;
using ;
using ;

//using before change the namespace
namespace
{
    class Execute
    {
        public static String run(String Command)
        {
            String Output = null;

            if (Command != null && !(""))
            {
                Process process = new Process();
                ProcessStartInfo processStartInfo = new ProcessStartInfo();
                = "";
                //no create the cmd windows
                = true;
                = true;
                = true;
                = true;
                = false;

                = processStartInfo;

                try
                {
                    ();
                    (Command);
                    ("exit");
                    (30 * 1000);
                    Output = ();
                }
                catch (Exception e)
                {
                    ();
                    return ();
                }
                finally
                {
                    ();
                }
            }

            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(, "");
            Regex regex_tar = new Regex("\r\n#end$");
            result = regex_tar.Replace(result,"");
            return result;
        }
    }
}