0

我正在处理服务器/客户端 TCP 通信,它工作正常,尽管我无法制作应用程序来将软件添加到 Windows 防火墙异常中。我确实找到了一个样本,但它没有用。

我的代码:

Imports NetFwTypeLib
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim appType As Type = Type.GetTypeFromProgID("HnetCfg.FwAuthorizedApplication")
        Dim app As INetFwAuthorizedApplication
        app = DirectCast(Activator.CreateInstance(appType), INetFwAuthorizedApplication)

        ' Set the application properties
        app.Name = "My App"
        app.ProcessImageFileName = "c:\users\klein\documents\visual studio 2012\projects\tcp_server\tcp_server\bin\debug\tcp_server.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
End Class

代码运行完美,未检测到异常。但是,加载表单后,程序并未列出。

谢谢你。

4

1 回答 1

0

看这个:

使用 C# 以编程方式将异常添加到 Windows 防火墙

于 2012-11-18T02:04:55.507 回答