0
我想開發一個代碼,使用vb.net管理防火牆端口。第一部分是列出所有啓用的端口。所以我想這個代碼:獲取開放端口列表,Windows防火牆
Function portsList()
Dim ports As INetFwOpenPorts
Dim port As INetFwOpenPort
Dim myPorts() As INetFwOpenPorts
Dim NetFwMgrType As Type = Type.GetTypeFromProgID("HNetCfg.FwMgr", False)
Dim mgr As INetFwMgr = DirectCast(Activator.CreateInstance(NetFwMgrType), INetFwMgr)
ports = DirectCast(mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts, INetFwOpenPorts)
Dim enumerate As System.Collections.IEnumerator = ports.GetEnumerator()
Dim i As Integer
While enumerate.MoveNext()
port = DirectCast(enumerate.Current, INetFwOpenPort)
myPorts(i) = port
i += 1
End While
Dim portAsString() As String
For j As Integer = 0 To i
portAsString(j) = myPorts(j).ToString
Next
Return portAsString
End Function
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim ports() As String = portsList()
Dim n As String = ports.Length
Dim newString As String = ""
For h As Integer = 0 To n
newString = ports(h) & vbNewLine
Next
RichTextBox1.Text = newString
End Sub
我想要做的就是名單Richtextbox1
所有端口點擊Button4
後。我得到的錯誤是:
NullReferenceException未解決。你調用的對象是空的。
我是Vb的新手,我怎麼能克服這個問題?
您是否考慮過掃描打開的端口,而不是從防火牆中獲取列表? – Sam 2013-05-08 10:12:39
thanx @Sam我沒想過。就像我說的,我想阻止(關閉)一些或所有這些端口摘錄幾個,我怎麼去在vb.net – 2013-05-08 14:36:28
看看[這裏](http://msdn.microsoft.com/en -us/library/aa366423.aspx)和[這裏](http://www.johnkoerner.com/index.php?/archives/49-Creating-a-Firewall-Exception-in-VB.Net.html)。順便說一下,掃描每個端口以查看其打開或關閉是否需要一段時間(遠程計算機上大約需要30分鐘)。 – Sam 2013-05-08 15:40:12