//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;
}
}
}
本文地址:https://www.stayed.cn/item/24749
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我