By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Enable and disable network adapter using Shell32.dll

P: n/a
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\System32 folder.

Comments are wellcome!
Enjoy...

using System;
using System.Collections.Generic;
using System.Text;
using Shell32;

namespace enabledisable
{
class Program
{
static void Main(string[] args)
{
Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder RootFolder =
sc.NameSpace(Shell32.ShellSpecialFolderConstants.s sfCONTROLS);
Shell32.Folder SrcFlder = null;
string Adapter = "Local Area Network";
ShellFolderItem fItem = null;

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

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

foreach (Shell32.FolderItem fi in SrcFlder.Items())
{
if (fi.Name == Adapter)
{
fItem = (ShellFolderItem)fi;
break;
}
}

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

foreach (Shell32.FolderItemVerb 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.WriteLine("Adapter was " +
tempStat.Replace("&", "") + "d");
return;
}
Console.WriteLine("Adapter wasn't found");
}
}
}
}
Sep 21 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.