Rss & SiteMap

Foxtable(狐表) http://www.foxtable.com

新一代数据库软件,完美融合Access、Foxpro、Excel、vb.net之优势,人人都能掌握的快速软件开发工具!
共2 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:如何获取已占用的端口号?

1楼
ygg8310 发表于:2023/9/27 7:37:00
如题:如何获取已占用的端口号?
2楼
有点蓝 发表于:2023/9/27 9:46:00
全局代码

Public Class TestPost
    
    Const PROCESS_ALL_ACCESS As Int32 = &H1F0FFF
    Const PROCESS_QUERY_INFORMATION As Int32 = &H00000400
    Const PROCESS_VM_READ As Int32 = &H0010
    Const PROCESS_VM_WRITE As Int32 = &H0020

    Public Declare Function QueryFullProcessImageNameW Lib "Kernel32.dll"(ByVal hProcess As Int32, ByVal flags As UInt32, ByVal nameList As Char(), ByRef nameLen As UInt32) As Int32

        Public Declare Function OpenProcess Lib "Kernel32.dll"(ByVal DesiredAccess As Int32, ByVal bInheritHandle As Boolean, ByVal ProcessId As Int32) As Int32

            Public Declare Function CloseHandle Lib "Kernel32.dll"(ByVal hObject As Int32) As Boolean

                Public Declare Function GetLastError Lib "Kernel32.dll"() As Int32
                    
                    
                    Public Function GetInfo() As List(Of String)
                        Dim infoList As New List(Of String)
                        Dim procInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()
                        Dim pro As System.Diagnostics.Process = New System.Diagnostics.Process()
                        pro.StartInfo.FileName = "cmd"
                        pro.StartInfo.UseShellExecute = False
                        pro.StartInfo.RedirectStandardInput = True
                        pro.StartInfo.RedirectStandardOutput = True
                        pro.StartInfo.RedirectStandardError = True
                        pro.StartInfo.CreateNoWindow = True
                        pro.Start()
                        pro.StandardInput.WriteLine("netstat -anob")
                        pro.StandardInput.WriteLine("exit")
                        Dim reg As New System.Text.RegularExpressions.Regex("\s+", RegexOptions.Compiled)
                        Dim line As String = Nothing
                        infoList.Clear()
                        
                        While (CSharpImpl.__Assign(line, pro.StandardOutput.ReadLine())) IsNot Nothing
                            line = line.Trim()
                            
                            If line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase) Then
                                line = reg.Replace(line, ",")
                                infoList.Add(line)
                            End If
                            
                            If line.StartsWith("UDP", StringComparison.OrdinalIgnoreCase) Then
                                line = reg.Replace(line, ",")
                                infoList.Add(line)
                            End If
                        End While
                        
                        pro.Close()
                        
                        Return infoList
                    End Function
                    
                    Private Class CSharpImpl
                        < Obsolete("Please refactor calling code to use normal Visual Basic assignment") > 
                        Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
                            target = value
                            Return value
                        End Function
                    End Class
                End Class


命令窗口测试

Dim a As New TestPost

Dim lst As List(Of String) = a.GetInfo()

For Each s As String In lst
    Output.Show(s)
Next
共2 条记录, 每页显示 10 条, 页签: [1]

Copyright © 2000 - 2018 foxtable.com Tel: 4000-810-820 粤ICP备11091905号

Powered By Dvbbs Version 8.3.0
Processed in .02405 s, 2 queries.