【发布时间】:2014-02-04 06:44:03
【问题描述】:
我正在使用 c# 开发一个小项目,我正在尝试在后台使用 windows 命令提示符运行命令(不显示命令提示符)。在使用之前,我已经在 vb.net 中完成了此操作: Shell("netsh wlan stop hostsnetwork", 0) ,但在 c# 中找不到如何操作。有人知道怎么做吗?
【问题讨论】:
我正在使用 c# 开发一个小项目,我正在尝试在后台使用 windows 命令提示符运行命令(不显示命令提示符)。在使用之前,我已经在 vb.net 中完成了此操作: Shell("netsh wlan stop hostsnetwork", 0) ,但在 c# 中找不到如何操作。有人知道怎么做吗?
【问题讨论】:
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;
}
【讨论】: