如何在visual c#中的管理员批准下运行以下命令?此外,我需要在运行控制台时隐藏控制台窗口。
感谢。
private void button5_Click(object sender, EventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C netsh wlan set hostednetwork mode=allow ssid=HotSpot key=12345678";
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
process.Start();
Process wifiStart = new Process();
ProcessStartInfo wifiStartInfo = new ProcessStartInfo();
wifiStartInfo.FileName = "netsh.exe";
wifiStartInfo.Arguments = "/C wlan start hostednetwork";
wifiStartInfo.Verb = "runas";
wifiStartInfo.UseShellExecute = true;
wifiStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = wifiStartInfo;
wifiStart.Start();
}
答案 0 :(得分:3)
Process.Start(new ProcessStartInfo {
FileName = "netsh",
Arguments = "wlan set hostednetwork mode=allow ssid=HotSpot key=12345678",
Verb = "runas",
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Hidden
});
这将使用“runas”动词启动一个进程,这会使shell尝试在提升权限模式下执行它。但是我们实际上需要shell首先参与其中,因此UseShellExecute = true
值。
最后一个属性告诉shell隐藏新进程的窗口,但我不确定这是否适用于控制台程序。