2012-11-18 84 views
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 

代码运行完美,没有发现异常。然而,该程序没有在表单加载后列出。

谢谢。

回答