zoukankan      html  css  js  c++  java
  • [.Net][转]dotNet取得各种系统信息

    Comes form http://blog.163.com/yinxin110/blog/static/1175896152009931101044166/

    一点工作笔记,供同志们参考与交流:


    If UBound(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then End '防止系统重载
    pLocalPath = System.Environment.CurrentDirectory ' My.Application.Info.DirectoryPath  '皆可取得当前运行目录
    pLocalHost = System.Environment.MachineName ' System.Net.Dns.GetHostName()  '皆可取得计算机名


        Public Function pGetLocalIP() As String '本地机器IP地址之有效网卡地址
            Dim Sql As String = System.Net.Dns.GetHostName()
            Dim IpVal As IPAddress()
            IpVal = System.Net.Dns.GetHostAddresses(Sql)
            pGetLocalIP = IpVal(0).ToString
            If IpVal.Length > 0 Then If Left(pGetLocalIP, 1) = "0" Then pGetLocalIP = IpVal(1).ToString '如果第一块网卡值无效,则取第二块网卡之IP地址,第三张网卡暂不考虑

        End Function


    Module publicUDPmdl
        Public pSktUDP As Socket = Nothing  '关于winsocket之UDP协议接收与发送信息模块
        Public pThdUDP As Thread

        Public Sub WaitUDPData() 'UDP 信息接收
            pSktUDP = New Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            Dim Srv As New Net.IPEndPoint(Net.IPAddress.Any, 8800)
            Dim IPs As String = pLocalIP '本地IP地址
            Dim Msg As String = ""

            Dim localEndPoint As New IPEndPoint(IPAddress.Parse(IPs), 8800) '指定IP和Port
            pSktUDP.Bind(localEndPoint)
            While (True)
                Dim Bytes(8192) As Byte  '需要定义在接收内,否则会保存上次之接收记录信息
                pSktUDP.ReceiveFrom(Bytes, Srv)
                IPs = Srv.Address.ToString
                Msg = System.Text.Encoding.Unicode.GetString(Bytes).ToString.Trim
                Call WriteReceiveData(Msg, IPs)
            End While
        End Sub

        Public Sub pStartUDPReceive()
            pThdUDP = New Thread(AddressOf WaitUDPData) '建立新的线程
            pThdUDP.Start()  '启动线程

        End Sub

        Public Sub pCloseUDPReceive()
            pSktUDP.Close()  '关闭Socket
            pThdUDP.Abort()  '中止线程

        End Sub

        Public Sub pUDPSendMsg(ByVal IPs As String, ByVal Msg As String) 'UDP 信息发送
            If PDIpVal(IPs) = False Then Exit Sub
            Dim SktA As New Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            Dim IPandPort As New IPEndPoint(IPAddress.Parse(IPs), 8800) '指定IP和Port

            SktA.Connect(IPandPort)
            SktA.Send(System.Text.Encoding.Unicode.GetBytes(Msg))
            SktA.Close()

        End Sub

    End Module


            If My.Computer.Network.Ping("192.168.1.101", 500) = True Then   'ping目标主机
                '目标计算机ping有回应'
            Else
                '目标计算机ping不通'
            End If  '注释:ping不通并不代表远程不存在或者不可用,可能对方禁止了icmp包


        Public Function pGetNetStat() As String '检测网络连接状态
            Dim Sql As String = ""
            If My.Computer.Network.IsAvailable = True Then
                Sql = "计算机已连接到网络上 √"
            Else
                Sql = "网络断开,计算机未连上"
            End If
            pGetNetStat = Sql

        End Function


        Public Function pGetDriveInfo() As String '硬磁盘之空间信息,计量单位为G
            Dim Cg As Long = System.IO.DriveInfo.GetDrives.Length
            Dim Ig As Long = 0
            Dim Sql As String = ""

            For Ig = 0 To Cg - 1
                Sql = Sql & System.IO.DriveInfo.GetDrives(Ig).ToString
                Sql = Sql & " 总容量:" & CLng(System.IO.DriveInfo.GetDrives(Ig).TotalSize / (1024 * 1024 * 1024)).ToString & " G"
                Sql = Sql & " 剩余空间:" & CLng(System.IO.DriveInfo.GetDrives(Ig).TotalFreeSpace / (1024 * 1024 * 1024)).ToString & " G" & vbCrLf
            Next
            pGetDriveInfo = Sql

        End Function



        Public Sub CloseChildForm(ByVal Frm As System.Windows.Forms.Form) ' 关闭此父窗体之所有子窗体。
            For Each ChildForm As Form In Frm.MdiChildren
                ChildForm.Close()
            Next
        End Sub


         Select Case pConnType  '数据库之连接字符串
             Case "1" 'Access库
                  pConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|MySoft110.dll;Jet OLEDB:DataBase Password=abcd"
             Case "2" 'Sql Server库
                  Dsn = GetFromINI("LoginNetDataBase", "SqlDatabase", Sql & "\InPara.ini")
                  pConnStr = "server=(local);database=" & Dsn & ";Trusted_Connection=SSPI;Connect Timeout=30"
             Case "3" 'oracle库
                   Hst = GetFromINI("LoginNetDataBase", "OrclUser", Sql & "\InPara.ini")
                   Wrd = GetFromINI("LoginNetDataBase", "OrclPwrd", Sql & "\InPara.ini")
                   Dsn = GetFromINI("LoginNetDataBase", "OrclServiceName", Sql & "\InPara.ini")
                   pConnStr = "Provider=OraOLEDB.Oracle.1;Data Source=" & Dsn & ";User ID=" & Hst & ";Password=" & Wrd & ";Unicode=True;Persist Security Info=True"

         End Select
  • 相关阅读:
    读《见识》 | 当别人扇了你一巴掌
    Java集合类
    Java数据结构简述
    Java加密算法
    Java JDK与JRE
    Java String、StringBuilder、StringBuffer[笔记]
    Java同步(Synchronization)
    Java断言(Assertion)
    Java strictfp
    Java Native Interface(JNI)
  • 原文地址:https://www.cnblogs.com/fjfjfjfjfjfj/p/1677146.html
Copyright © 2011-2022 走看看