2011-10-31 56 views
0

我想用Visual Basic 6.0填充所有安裝的網卡名稱的組合框。有沒有辦法做到這一點?我也想知道如何添加,編輯和刪除註冊表的值?獲取安裝的網卡列表

回答

1

最簡單的方法是將shell輸出到ipconfig命令,將輸出重定向到文件,然後解析文件。有許多通常稱爲「ShellAndWait()」的函數的實現。我已經拿走了一個我曾經松鼠 - 它可能不是最好的,但它的工作。

Option Explicit 

Private Declare Function CloseHandle Lib "Kernel32.dll" (_ 
    ByVal hHandle As Long _ 
) As Long 

Private Declare Function OpenProcess Lib "Kernel32.dll" (_ 
    ByVal dwDesiredAccess As Long, _ 
    ByVal bInheritHandle As Long, _ 
    ByVal dwProcessId As Long _ 
) As Long 

Private Declare Function WaitForSingleObject Lib "Kernel32.dll" (_ 
    ByVal hHandle As Long, _ 
    ByVal dwMilliseconds As Long _ 
) As Long 


Private Const INFINITE As Long = -1& 
Private Const SYNCHRONIZE As Long = &H100000 

Private Sub Form_Load() 

    Dim oNetworkAdapters As VBA.Collection 
    Dim vNetworkAdapter As Variant 

    Set oNetworkAdapters = GetNetworkAdapters() 

    cmbNICs.Clear 
    For Each vNetworkAdapter In oNetworkAdapters 
     cmbNICs.AddItem vNetworkAdapter 
    Next vNetworkAdapter 

End Sub 

Public Function GetNetworkAdapters() As VBA.Collection 

    Dim sTempFileName As String 
    Dim nFileNo   As Integer 
    Dim sLine   As String 
    Dim oNetworkAdapters As VBA.Collection 

    Set oNetworkAdapters = New VBA.Collection 

    sTempFileName = Environ$("TEMP") & "\VBTmp" & Format$(Now, "yyyymmddhhnnss") 

    If ShellAndWait("cmd.exe /c ipconfig > """ & sTempFileName & """", vbHide) Then 

     nFileNo = FreeFile 

     Open sTempFileName For Input As #nFileNo 

     Do Until EOF(nFileNo) 
      Line Input #nFileNo, sLine 
      If Len(sLine) > 0 Then 
       If sLine Like "*:" Then 
        If Not sLine Like " *:" Then 
         oNetworkAdapters.Add sLine 
        End If 
       End If 
      End If 
     Loop 

     Close #nFileNo 

     Kill sTempFileName 

    End If 

    Set GetNetworkAdapters = oNetworkAdapters 

End Function 

' Start the indicated program and wait for it to finish, hiding while we wait. 
Public Function ShellAndWait(ByRef in_sProgramName As String, _ 
          ByVal in_enmWindowStyle As VbAppWinStyle) As Boolean 

    Dim nProcessId  As Long 
    Dim hProcess  As Long 

    ' Start the program. 
    On Error GoTo ShellError 
    nProcessId = Shell(in_sProgramName, in_enmWindowStyle) 
    On Error GoTo 0 

    DoEvents 

    ' Wait for the program to finish. 
    ' Get the process handle. 
    hProcess = OpenProcess(SYNCHRONIZE, 0, nProcessId) 
    If hProcess <> 0 Then 
     WaitForSingleObject hProcess, INFINITE 
     CloseHandle hProcess 
    End If 

    ShellAndWait = True 

    Exit Function 

ShellError: 
    MsgBox "Error starting task '" & in_sProgramName & "'" & vbCrLf & Err.Description, vbOKOnly Or vbExclamation, "Error" 
End Function 
0

下面是簡單的代碼,將檢測所有的以太網和無線適配器

NetworkInterface slectedNic; 
IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().Where(network => network.OperationalStatus == OperationalStatus.Up && (network.NetworkInterfaceType == NetworkInterfaceType.Ethernet || network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)); 
foreach (NetworkInterface item in nics) 
     { 
      cmbAdptors.Items.Add(item); 
     } 

但如果u要檢測纔有效無線適配器

變化

.Where(network => network.OperationalStatus == OperationalStatus.Up && (network.NetworkInterfaceType == NetworkInterfaceType.Ethernet || network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)); 

.Where(network => network.OperationalStatus == OperationalStatus.Up && network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) 
相關問題