Enable and disable network adapter using Shell32.dll

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eran.Yasso@gmail.com

    Enable and disable network adapter using Shell32.dll

    Hi all,

    The following code sets/disables network adpter's status. Since I have
    no idea where to put this code and I wish to share the community with
    it. I did rverse engineering from VBS to C#.
    To use this code, simply add reference to Shell32.dll located in
    WINDOWS\System3 2 folder.

    Comments are wellcome!
    Enjoy...

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using Shell32;

    namespace enabledisable
    {
    class Program
    {
    static void Main(string[] args)
    {
    Shell32.ShellCl ass sc = new Shell32.ShellCl ass();
    Shell32.Folder RootFolder =
    sc.NameSpace(Sh ell32.ShellSpec ialFolderConsta nts.ssfCONTROLS );
    Shell32.Folder SrcFlder = null;
    string Adapter = "Local Area Network";
    ShellFolderItem fItem = null;

    foreach (Shell32.Folder Item2 fi in RootFolder.Item s())
    {
    if (fi.Name == "Network Connections")
    {
    SrcFlder = (Shell32.Folder )fi.GetFolder;
    break;
    }
    }

    if (SrcFlder == null)
    {
    Console.WriteLi ne("SrcFlder \"Network Connections\"
    doesn't exist");
    return;
    }

    foreach (Shell32.Folder Item fi in SrcFlder.Items( ))
    {
    if (fi.Name == Adapter)
    {
    fItem = (ShellFolderIte m)fi;
    break;
    }
    }

    if (fItem == null)
    {
    Console.WriteLi ne("Adapter \"" + Adapter + "\" doesn't
    exist");
    return;
    }

    foreach (Shell32.Folder ItemVerb fi in fItem.Verbs())
    {
    string tempStat = string.Empty;
    //0 - to disable adapter
    //1 - to enable adapter
    int newState = 1;
    switch (newState)
    {
    case 0:
    tempStat = "disa&ble";
    newState = 22;
    break;
    case 1:
    tempStat = "en&able";
    newState = 0;
    break;
    }

    if (string.Compare (fi.Name, tempStat, true) == 0)
    {
    //set adapter's state
    fi.DoIt();
    Console.WriteLi ne("Adapter was " +
    tempStat.Replac e("&", "") + "d");
    return;
    }
    Console.WriteLi ne("Adapter wasn't found");
    }
    }
    }
    }
Working...