十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这儿有一个C#的参考
成都创新互联专注于顺义企业网站建设,响应式网站开发,商城网站建设。顺义网站建设公司,为顺义等地区提供建站服务。全流程按需网站设计,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
我给改写成了VB点虐 看看你能用上不?
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Module mdlIpDns
Public Function RunCMD(ByVal FileName As String, ByVal Arguments As String, ByVal RecordLog As Boolean) As String
REM 运行一个控制台程序并返回其输出参数
Try
If RecordLog = True Then Trace.WriteLine(FileName + " " + Arguments)
Dim myProc As Process = New Process
myProc.StartInfo.FileName = FileName
myProc.StartInfo.CreateNoWindow = True
myProc.StartInfo.Arguments = Arguments
myProc.StartInfo.RedirectStandardOutput = True
myProc.StartInfo.UseShellExecute = False
myProc.Start()
Dim myStreamReader As System.IO.StreamReader = New System.IO.StreamReader(myProc.StandardOutput.BaseStream, System.Text.Encoding.Default)
Dim myText As String = myStreamReader.ReadToEnd
myStreamReader.Close()
If RecordLog = True Then Trace.WriteLine(myText)
If Not myProc.HasExited Then myProc.Kill()
Return myText
Catch ex As Exception
Trace.WriteLine(ex.ToString)
Return vbNull
End Try
End Function
Public Function GetAllIp() As String
REM 获取本机全部网卡的IP地址
GetAllIp = ""
Dim Address() As System.Net.IPAddress
Dim i As Integer
Address = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList
For i = 0 To UBound(Address)
GetAllIp = GetAllIp (Chr(13) Address(i).ToString())
Next
End Function
Public Function GetPrimaryDNS() As String
REM 获取本机主DNS
GetPrimaryDNS = ""
Dim m As System.Text.RegularExpressions.Match
Dim myResult As String = RunCMD("nslookup", "", True) '需要RunCMD.mdl
m = Regex.Match(myResult, "\d+\.\d+\.\d+\.\d+")
If m.Success Then
Return m.Value
Else
Return vbNull
End If
End Function
Public Function GetLocalIP() As String
REM 获取本机正在使用的IPv4地址
REM 1.一个电脑有多个网卡,有线的、无线的、还有VMare虚拟的两个网卡。
REM 2.就算只有一个网卡,但是该网卡配置了N个IP地址.其中还包括IPv6地址。
REM 3.下面通过查询本机路由表,获取访问默认网关时使用的网卡IP。
Dim m As System.Text.RegularExpressions.Match
Dim myResult As String = RunCMD("route", "print", True) '需要RunCMD.mdl
m = Regex.Match(myResult, "0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)")
If m.Success Then
Return m.Groups(2).Value
Else
Try
Dim myTcpClient As System.Net.Sockets.TcpClient = New System.Net.Sockets.TcpClient
myTcpClient.Connect("", 80) '连接百度判断IP
Dim myIpPoint As System.Net.IPEndPoint = myTcpClient.Client.LocalEndPoint
Dim myIp As String = myIpPoint.Address.ToString
myTcpClient.Close()
Return myIp
Catch ex As Exception
Return vbNull
End Try
End If
End Function
End Module
My功能可以替代很多API,因为CLR已经封装了大量系统函数,不建议使用API,那样会限制程序的可移植性。(微软声称.NET FRAMEWORK是跨平台的,但是迄今都没看见在Linux上的.NET 程序)
多翻翻MSDN就知道东西怎么用了
VB获取计算机名称的方法如下:
说明:计算机名就是打开控制面板-系统-网络标识-完整的计算机名称
Private Declare Function GetComputerName Lib "kernel32"
Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Command1_Click()
Dim Name As String, Length As Long
Length = 225
Name = String(Length, Chr(0))
GetComputerName Name, Length
Name = Left(Name, Length)
Label1.Caption = Name
End Sub
Private Sub Form_Load()
End Sub
注意:该方法是通过kernel32.dll来获取的,chr(0)返回的就是计算机名。
kernel32.dll是Windows9x/Me中非常重要的32位动态链接库文件,属于内核级文件。它控制着系统的内存管理、数据的输入输出操作和中断处理,当Windows启动时,kernel32.dll就驻留在内存中特定的写保护区域,使别的程序无法占用这个内存区域。
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub GetName()
Dim sJM As String
Dim sMC As String
Dim sIP As String
Dim s As String
s = String(128, Chr(0))
GetComputerName s, Len(s)
s = UCase(Left(s, InStr(1, s, Chr(0)) - 1))
sJM = s'计算机名称
s = String(128, Chr(0))
GetUserName s, Len(s)
s = UCase(Left(s, InStr(1, s, Chr(0)) - 1))
sMC = s‘当前用户名称
End Sub
'用这种方法在远程获得再发回本机就可以吧
'取计算机型号暂时不会