2014-01-15 331 views
-1

我正在处理c#中的一个小项目,并且试图在后台使用windows命令提示符运行命令(不显示命令提示符)。香港专业教育学院在vb.net中使用这样做过:壳牌(“netsh的WLAN停止hostednetwork”,0),但无法找到如何做到这一点在C#。任何人都知道吗?如何在c#中使用命令提示符运行命令#

回答

2
public static string ProcessStarter(string processName, string argString, string workingDirectory = null) 
    { 
     var prs = new Process(); 
     if (!string.IsNullOrWhiteSpace(workingDirectory)) 
     { 
      prs.StartInfo.WorkingDirectory = workingDirectory; 
     } 
     prs.StartInfo.UseShellExecute = false; 
     prs.StartInfo.RedirectStandardOutput = true; 
     prs.StartInfo.RedirectStandardError = true; 
     prs.StartInfo.FileName = processName; 
     prs.StartInfo.Arguments = argString; 
     // LOOK HERE 
     prs.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     prs.Start(); 
     string result = prs.StandardOutput.ReadToEnd(); 
     string resultErr = prs.StandardError.ReadToEnd(); 
     return string.IsNullOrEmpty(result) ? resultErr : result; 
} 
相关问题