×

# windows添加程序防火墙规则

hqy hqy 发表于2026-04-08 17:07:15 浏览7 评论0

抢沙发发表评论

# windows添加程序防火墙规则

在windows下,命令创建防火墙规则,使程序能通过防火墙

命令

C#
public sealed class Command{    public static string Windows(string arg, string[] commands)    {        return Execute("cmd.exe", arg, commands);    }    public static string Execute(string fileName, string arg, string[] commands)    {        Process proc = new Process();        proc.StartInfo.CreateNoWindow = true;        proc.StartInfo.FileName = fileName;        proc.StartInfo.UseShellExecute = false;        proc.StartInfo.RedirectStandardError = true;        proc.StartInfo.RedirectStandardInput = true;        proc.StartInfo.RedirectStandardOutput = true;        proc.StartInfo.Arguments = arg;        proc.StartInfo.Verb = "runas";        proc.Start();        if (commands.Length > 0)        {            for (int i = 0; i < commands.Length; i++)            {                proc.StandardInput.WriteLine(commands[i]);            }        }        proc.StandardInput.AutoFlush = true;        proc.StandardInput.WriteLine("exit");        string output = proc.StandardOutput.ReadToEnd();        proc.StandardError.ReadToEnd();        proc.WaitForExit();        proc.Close();        proc.Dispose();        return output;    }}

创建

C#
string content = @"@echo offcd  ""%CD%""for /f ""tokens=4,5 delims=. "" %%a in ('ver') do if %%a%%b geq 60 goto new:oldcmd /c netsh firewall delete allowedprogram program=""%CD%\可执行程序.exe"" profile=ALLcmd /c netsh firewall add allowedprogram program=""%CD%\可执行程序.exe"" name=""规则名"" ENABLEcmd /c netsh firewall add allowedprogram program=""%CD%\可执行程序.exe"" name=""规则名"" ENABLE profile=ALLgoto end:newcmd /c netsh advfirewall firewall delete rule name=""规则名""cmd /c netsh advfirewall firewall add rule name=""规则名"" dir=in action=allow program=""%CD%\可执行程序.exe"" protocol=tcp enable=yes profile=publiccmd /c netsh advfirewall firewall add rule name=""规则名"" dir=in action=allow program=""%CD%\可执行程序.exe"" protocol=udp enable=yes profile=publiccmd /c netsh advfirewall firewall add rule name=""规则名"" dir=in action=allow program=""%CD%\可执行程序.exe"" protocol=tcp enable=yes profile=domaincmd /c netsh advfirewall firewall add rule name=""规则名"" dir=in action=allow program=""%CD%\可执行程序.exe"" protocol=udp enable=yes profile=domaincmd /c netsh advfirewall firewall add rule name=""规则名"" dir=in action=allow program=""%CD%\可执行程序.exe"" protocol=tcp enable=yes profile=privatecmd /c netsh advfirewall firewall add rule name=""规则名"" dir=in action=allow program=""%CD%\可执行程序.exe"" protocol=udp enable=yes profile=private:end";System.IO.File.WriteAllText("firewall.bat", content);Command.Execute("firewall.bat", string.Empty, new string[0]);


打赏

本文链接:https://kinber.cn/post/6425.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客