首页  编辑  

启用禁止网络连接

Tags: /超级猛料/Network.网络通讯/其它/   Date Created:

http://beta.experts-exchange.com/Programming/Q_20648190.html

Accepted Answer from jrocnuck  Date: 06/15/2003 10:43AM PDT  

vb.net is just an extension of VB...    

this is it in VB... in the example I have 4 separate local area connections..

Const ssfCONTROLS = 3

Sub MyDisableSubroutine()

 Debug.Print "Hello dude.. we are disabling the Local Area Connections!" & Chr$(13)

 DisableConnection ("Local Area Connection")

 DisableConnection ("Local Area Connection 1")

 DisableConnection ("Local Area Connection 21")

 DisableConnection ("Local Area Connection 60")

End Sub

Sub MyEnableSubroutine()

 Debug.Print "Hello dude.. we are enabling the Local Area Connections!" & Chr$(13)

 EnableConnection ("Local Area Connection")

 EnableConnection ("Local Area Connection 1")

 EnableConnection ("Local Area Connection 21")

 EnableConnection ("Local Area Connection 60")

End Sub

Function DisableConnection(sConnectionName)

' This is the string of the verb (i.e. Context Menu we want to find for the connection)

' if this string doesn't exist, then the connection is already disabled!

sVerb = "Disa&ble"

Set shellApp = CreateObject("shell.application")

Set oControlPanel = shellApp.Namespace(ssfCONTROLS)

Set oDisableVerb = Nothing

Set oNetConnections = Nothing

Set oLanConnection = Nothing

For Each folderitem In oControlPanel.items

     If folderitem.Name = "Network and Dial-up Connections" Or "Network Connections" Then

         Set oNetConnections = folderitem.getfolder: Exit For

     End If

Next

If oNetConnections Is Nothing Then

MsgBox "Couldn't find 'Network and Dial-up Connections' folder"

Exit Function

End If

For Each folderitem In oNetConnections.items

 If LCase(folderitem.Name) = LCase(sConnectionName) Then

     Set oLanConnection = folderitem: Exit For

 End If

Next

If oLanConnection Is Nothing Then

 MsgBox "Couldn't find '" & sConnectionName & "' item"

 Exit Function

End If

For Each Verb In oLanConnection.verbs

 If Verb.Name = sVerb Then

     Verb.DoIt

 End If

Next

End Function

Function EnableConnection(sConnectionName)

' This is the string of the verb (i.e. Context Menu we want to find for the connection)

' if this string doesn't exist, then the connection is already enabled!

sVerb = "En&able"

Set shellApp = CreateObject("shell.application")

Set oControlPanel = shellApp.Namespace(ssfCONTROLS)

Set oDisableVerb = Nothing

Set oNetConnections = Nothing

Set oLanConnection = Nothing

For Each folderitem In oControlPanel.items

     If folderitem.Name = "Network and Dial-up Connections" Or "Network Connections" Then

         Set oNetConnections = folderitem.getfolder: Exit For

     End If

Next

If oNetConnections Is Nothing Then

MsgBox "Couldn't find 'Network and Dial-up Connections' folder"

Exit Function

End If

For Each folderitem In oNetConnections.items

 If LCase(folderitem.Name) = LCase(sConnectionName) Then

     Set oLanConnection = folderitem: Exit For

 End If

Next

If oLanConnection Is Nothing Then

 MsgBox "Couldn't find '" & sConnectionName & "' item"

 Exit Function

End If

For Each Verb In oLanConnection.verbs

 If Verb.Name = sVerb Then

     Verb.DoIt

 End If

Next

End Function  

---------------------------------------

const

  discVerb = ' 禁用(&B)';

  connVerb = '启用(&A)';

function DisableEthernet(const EthName: String): Boolean;

var

 cpFolder: Folder;

 nwFolder: Folder;

 nVerbs: FolderItemVerbs;

 i,j,k: integer;

begin

 result := false;

 cpFolder := shell1.NameSpace(3);

 if cpFolder <> nil then

 begin

   for i := 0 to cpFolder.items.Count-1 do

   begin

     if cpFolder.Items.Item(i).Name = '网络和拨号连接' then

     begin

       nwFolder := cpFolder.items.item(i).GetFolder as Folder;

       if nwFolder <> nil then

       begin

         for j :=0 to nwFolder.items.Count-1 do

         begin

           if nwFolder.Items.Item(j).Name = EthName then

           begin

             nVerbs := nwFolder.Items.Item(j).Verbs;

             for k := 0 to  nVerbs.Count-1 do

       begin

               if nVerbs.Item(k).Name = discVerb then

               begin

                 nVerbs.Item(k).DoIt;  

                 Result := True;

               end;

             end;

           end;

         end;

       end;

     end;

   end;

   if nwFolder = nil then

      showmessage('Network and Dial-up Connections not found');

 end

 else

   showmessage('control panel not found');

end;

function EnableEthernet(const EthName: String): Boolean;

var

 cpFolder: Folder;

 nwFolder: Folder;

 nVerbs: FolderItemVerbs;

 i,j,k: integer;

begin

 result := false;

 cpFolder := shell1.NameSpace(3);

 if cpFolder <> nil then

 begin

   for i := 0 to cpFolder.items.Count-1 do

   begin

     if cpFolder.Items.Item(i).Name = '网络和拨号连接' then

     begin

       nwFolder := cpFolder.items.item(i).GetFolder as Folder;

       if nwFolder <> nil then

       begin

         for j :=0 to nwFolder.items.Count-1 do

         begin

           if nwFolder.Items.Item(j).Name = EthName then

           begin

             nVerbs := nwFolder.Items.Item(j).Verbs;

             for k := 0 to  nVerbs.Count-1 do

       begin

               if nVerbs.Item(k).Name = connVerb then

               begin

                 nVerbs.Item(k).DoIt;  

                 Result := True;

               end;

             end;

           end;

         end;

       end;

     end;

   end;

   if nwFolder = nil then

      showmessage('Network and Dial-up Connections not found');

 end

 else

   showmessage('control panel not found');

end;