C# Netsh命令无法在文本框中显示正确的结果
我对命令方法和重置方法有问题。如果我尝试执行它们,我会在RichTextBox中收到消息 在ipv4上设置全局命令失败参数不正确 但如果我运行netsh interface tcp show global或点击Check方法,它们会通知我状态正在更改 这是按钮C# Netsh命令无法在文本框中显示正确的结果,c#,admin,netsh,C#,Admin,Netsh,我对命令方法和重置方法有问题。如果我尝试执行它们,我会在RichTextBox中收到消息 在ipv4上设置全局命令失败参数不正确 但如果我运行netsh interface tcp show global或点击Check方法,它们会通知我状态正在更改 这是按钮 class TcpIpCommands { public string Check() { Process p = new Process();
class TcpIpCommands
{
public string Check()
{
Process p = new Process();
p.StartInfo.FileName = @"C:\Windows\syswow64\netsh.exe";
p.StartInfo.Arguments = "int tcp show global";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(737);
p.Start();
p.WaitForExit();
return p.StandardOutput.ReadToEnd();
}
public string Command(string FileName, string Arguments)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = FileName;
p.Arguments = Arguments;
p.UseShellExecute = false;
p.CreateNoWindow = true;
p.RedirectStandardOutput = true;
p.StandardOutputEncoding = Encoding.GetEncoding(737);
using (Process process = Process.Start(p))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
return result;
}
}
}
public string Reseting()
{
Process p = new Process();
p.StartInfo.FileName = @"C:\Windows\syswow64\netsh.exe";
p.StartInfo.Arguments = @"interface tcp reset";
p.StartInfo.Verb = "runas";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(737);
p.Start();
p.WaitForExit();
return p.StandardOutput.ReadToEnd();
}
}
我无法理解为什么RichTextBox显示该消息,但进程成功执行。
一张便条。我已尝试使用运行命令
private void BtnCheck_Click(object sender, EventArgs e)
{
TcpIpCommands tic = new TcpIpCommands();
richTextBox1.Text = tic.Check();
}
private void btnChimney_Click(object sender, EventArgs e)
{
TcpIpCommands tic = new TcpIpCommands();
richTextBox1.Text = tic.Command(@"C:\Windows\system32\netsh.exe", "interface tcp set global chimney=enabled");
}
private void BtnReset_Click(object sender, EventArgs e)
{
TcpIpCommands tic = new TcpIpCommands();
richTextBox1.Text = tic.Reseting();
}
而且都是从
C:\Windows\System32\netsh.exe
及
C:\Windows\syswow64\netsh.exe
我得到了相同的错误“set global command failed on ipv4参数不正确”,但代码执行并更改了参数。我添加了一个清单,现在程序要求提升权限,但什么也没有。点击烟囱按钮,在文本框上显示“在ipv4上设置全局命令失败,参数不正确”,参数不正确的值从禁用变为启用。注意,我在Win7 64和Win 10 64中对此进行了检查。我现在看到的只是ipv6参数没有生效,只有ipv4参数发生了变化。
StartInfo.Verb = "runas";