如何使用 vb.net 代码将我的应用程序添加到 Windows 防火墙的例外列表中?
谢谢
Windows Vista 和 7 提供了一个相当强大的防火墙 API,可用于向防火墙添加例外。如果代码以管理员权限运行,则下面的代码将为指定应用程序向 Windows 防火墙添加例外。向您的应用程序添加对%systemroot%\system32\FirewallAPI.dll的引用。
Imports NetFwTypeLib
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Create the Application we want to add to the exception list
Dim appType As Type = Type.GetTypeFromProgID("HnetCfg.FwAuthorizedApplication")
Dim app As INetFwAuthorizedApplication
app = DirectCast(Activator.CreateInstance(appType), INetFwAuthorizedApplication)
' Set the application properties
app.Name = "Negative0's Sandbox"
app.ProcessImageFileName = "C:\Users\Negative0\vbsandbox2.exe"
app.Enabled = True
' Get the firewall manager, so we can get the list of authorized apps
Dim fwMgrType As Type = Type.GetTypeFromProgID("HnetCfg.FwMgr")
Dim fwMgr As INetFwMgr
fwMgr = DirectCast(Activator.CreateInstance(fwMgrType), INetFwMgr)
' Get the list of authorized applications from the Firewall Manager, so we can add our app to that list
Dim apps As INetFwAuthorizedApplications
apps = fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications
apps.Add(app)
End Sub