我的操作系统是XP,有两块网卡,编程工具是C#,请问,如何用程序实现启用、禁用网卡呢?具体的说就是启用第一块网卡的时候,禁用第二块网卡;如果启用第二块网卡的时候,禁用第一块网卡,希望能提供关键代码,多谢。

解决方案 »

  1.   

    //添加引用-COM-浏览-C:\WINDOWS\system32\shell32.dll-添加private bool SetNetworkAdapter(bool status)
    {
    const string discVerb = "禁用(&B)";
    const string connVerb = "启用(&A)";
    const string network = "网络和拨号连接";
    const string networkConnection = "本地连接";
    string sVerb; if (status)
    {

    sVerb = connVerb;
    }
    else
    {

    sVerb = discVerb;
    } Shell32.Shell sh = new Shell32.Shell();
    Shell32.Folder folder;

    folder = sh.NameSpace(3);   //Shell32.ShellSpecialFolderConstants.ssfCONTROLS
    try
    {
    //进入控制面板的所有选项
    foreach (Shell32.FolderItem myItem in folder.Items())
    {
    //进入网络和拔号连接
    if (myItem.Name == network)
    {
    Shell32.Folder fd = (Shell32.Folder)myItem.GetFolder;
    foreach (Shell32.FolderItem fi in fd.Items())
    {
    //找到本地连接
    if (fi.Name.IndexOf(networkConnection) > -1)
    {

    //找本地连接的所有右键功能菜单
    foreach (Shell32.FolderItemVerb Fib in fi.Verbs())
    {
    if (Fib.Name == sVerb) 
    {
    Fib.DoIt();

    break;
    }
    }
    }
    }
    }
    }
    }
    catch (Exception e)
    {
    MessageBox.Show(e.Message);
    return false;
    }
    ;
    return true;
    }