獲取本機物理地址

第一種:只拿到純數字的

'獲取物理地址
            Dim strMacAddress As String = String.Empty
            Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
            strMacAddress = nics(0).GetPhysicalAddress.ToString
           

第二種:中間有連接符號

'獲取物理地址
            Dim macs As List(Of String) = New List(Of String)

            Dim startInfo As ProcessStartInfo = New ProcessStartInfo("ipconfig", "/all")

            startInfo.UseShellExecute = False
            startInfo.RedirectStandardInput = True
            startInfo.RedirectStandardOutput = True
            startInfo.RedirectStandardError = True
            startInfo.CreateNoWindow = True

            Dim p As Process = Process.Start(startInfo) '截取輸出流 
            Dim reader As StreamReader = p.StandardOutput

            Dim line As String = reader.ReadLine()
            While Not reader.EndOfStream
                If Not IsNothing(line) Then
                    line = line.Trim()
                    If line.StartsWith("物理地址") Then
                        macs.Add(line.Substring(line.IndexOf(":") + 1))
                    End If
                End If
                line = reader.ReadLine()
            End While

            '等待程序執行完退出進程   
            p.WaitForExit()
            p.Close()
            reader.Close()
            strMacValue = macs(0)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章