用Delphi实现关闭、重启本地网络连接

  1. unit Unit1;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, OleServer, Shell32_TLB, StdCtrls;
  6. const
  7.   connVerb = '启用';
  8.   discVerb = '停用';
  9. type
  10.   TForm1 = class(TForm)
  11.     shl1: TShell;
  12.     btn1: TButton;
  13.     btn2: TButton;
  14.     procedure btn1Click(Sender: TObject);
  15.     procedure btn2Click(Sender: TObject);
  16.   private
  17.     { Private declarations }
  18.     function ControlEthernet(const EthName, FolderItemVerbsName: string): Boolean;
  19.   public
  20.     { Public declarations }
  21.   end;
  22. var
  23.   Form1: TForm1;
  24. implementation
  25. {$R *.dfm}
  26. {
  27. Component -> Import ActiveX Control -> Microsoft Shell Contolrs And  Automation
  28. 然后创建单元文件,之后再新建一个安装包,添加该单元文件,最后是安装该控件,就可以用了(Tshell)
  29. }
  30. procedure TForm1.btn1Click(Sender: TObject);
  31. begin
  32.   ControlEthernet('本地连接', discVerb); //启用本地连接 '停用&'
  33. end;
  34. procedure TForm1.btn2Click(Sender: TObject);
  35. begin
  36.   ControlEthernet('本地连接', connVerb); //启用本地连接 '启用&'
  37. end;
  38. function TForm1.ControlEthernet(const EthName,
  39.   FolderItemVerbsName: string): Boolean;
  40. var cpFolder, nwFolder: Folder; //一个外壳文件夹对象
  41.   nVerbs: FolderItemVerbs; //获得上下文相关的菜单信息
  42.   i, j, k: integer;
  43.   Shell1: TShell;
  44. begin
  45.   Result := false;
  46.   Shell1 := TShell.Create(Application);
  47.   cpFolder := Shell1.NameSpace(3); //选择控件面板
  48.   if cpFolder <> nil then
  49.   begin
  50.     for i := 0 to cpFolder.items.Count - 1 do //返回它所包含的外壳对象的集合(文件) 28
  51.     begin
  52.       if cpFolder.Items.Item(i).Name = '网络连接' then //返回的集合的名称
  53.       begin
  54.         nwFolder := cpFolder.items.item(i).GetFolder as Folder; //取得该cpFolder下面的外壳对象
  55.         if nwFolder <> nil then //内容不为空
  56.         begin
  57.           for j := 0 to nwFolder.items.Count - 1 do //历遍cpFolder下面的外壳对象
  58.           begin
  59.             if nwFolder.Items.Item(j).Name = EthName then //若果为'本地连接'
  60.             begin
  61.               nVerbs := nwFolder.Items.Item(j).Verbs; //取得该对象的上下文菜单信息
  62.               for k := 0 to nVerbs.Count - 1 do //历遍所有菜单信息
  63.               begin
  64.                 if Pos(FolderItemVerbsName, nVerbs.Item(k).Name) > 0 then //如果菜单名称为 '禁用&' 时,
  65.                                                                //  则执行该菜单命令
  66.                 begin
  67.                   nVerbs.Item(k).DoIt; //执行该菜单命令
  68.                   //nwFolder.Items.Item(j).InvokeVerb(nwFolder.Items.Item(j).Verbs.Item(k).Name);
  69.                   Result := true//效果一致
  70.                 end;
  71.               end;
  72.             end;
  73.           end;
  74.         end;
  75.       end;
  76.     end;
  77.   end;
  78. end;
  79. end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值