c#尝试执行cmd命令打开防火墙端口-sql端口

c#尝试执行cmd命令打开防火墙端口-sql端口,c#,C#,我是新来的,需要一些帮助。我正在尝试创建一个按钮,它将打开sql端口。我有此代码,但无法在入站规则中打开端口 private void addPorts_Click(object sender, EventArgs e) { System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new Pr

我是新来的,需要一些帮助。我正在尝试创建一个按钮,它将打开sql端口。我有此代码,但无法在入站规则中打开端口

private void addPorts_Click(object sender, EventArgs e)
        {
            System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
            myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
            myProcessInfo.Arguments = @"netsh advfirewall firewall add rule name= ""Open Port 80"" dir=in action=allow protocol=TCP localport=80

@echo ========= SQL Server Ports  ===================
@echo Enabling SQLServer default instance port 1433
netsh advfirewall firewall add rule name = "" SQL Server"" dir =in action = allow protocol = TCP localport = 1433
@echo Enabling Dedicated Admin Connection port 1434
netsh advfirewall firewall add rule name = ""SQL Admin Connection"" dir =in action = allow protocol = TCP localport = 1434
@echo Enabling Conventional SQL Server Service Broker port 4022
netsh advfirewall firewall add rule name = ""SQL Service Broker"" dir =in action = allow protocol = TCP localport = 4022
@echo Enabling Transact SQL/ RPC port 135
netsh advfirewall firewall add rule name = ""SQL Debugger/RPC"" dir =in action = allow protocol = TCP localport = 135
@echo ========= Analysis Services Ports  ==============
@echo Enabling SSAS Default Instance port 2383
netsh advfirewall firewall add rule name = ""Analysis Services"" dir =in action = allow protocol = TCP localport = 2383
@echo Enabling SQL Server Browser Service port 2382
netsh advfirewall firewall add rule name = ""SQL Browser"" dir =in action = allow protocol = TCP localport = 2382

@echo ========= Misc Applications ==============
@echo Enabling HTTP port 80
netsh advfirewall firewall add rule name = ""HTTP"" dir =in action = allow protocol = TCP localport = 80
@echo Enabling SSL port 443
netsh advfirewall firewall add rule name = ""SSL"" dir =in action = allow protocol = TCP localport = 443
@echo Enabling port for SQL Server Browser Service's 'Browse' Button
netsh advfirewall firewall add rule name = ""SQL Browser"" dir =in action = allow protocol = UDP localport = 1434
@echo Allowing multicast broadcast response on UDP(Browser Service Enumerations OK)
netsh firewall set multicastbroadcastresponse ENABLE";
            myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
            myProcessInfo.Verb = "runas"; //The process should start with elevated permissions
            System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo
        }

“无法在入站规则中打开端口”:为什么不能?必须像管理员一样运行应用程序。代码不会改变这一点。。如果您无法以管理员身份运行。。你不能。“无法在入站规则中打开端口”:为什么不能?必须像管理员一样运行应用程序。代码不会改变这一点。。如果您无法以管理员身份运行。。你不能。
private void addPorts_Click(object sender, EventArgs e)
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            process.StandardInput.WriteLine(@"
@echo Enabling SQLServer default instance port 1433
netsh advfirewall firewall add rule name =""SQLServer"" dir =in action = allow protocol = TCP localport = 1433
@echo Enabling Dedicated Admin Connection port 1434");
            process.StandardInput.Flush();
            process.StandardInput.Close();
            process.WaitForExit();
            Console.WriteLine(process.StandardOutput.ReadToEnd());
            //Console.ReadKey();
            MessageBox.Show("Succesful!", "Succesful!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }