473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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\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
0 4239

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Derek White | last post by:
Ok here's the deal. We are finally getting DSL again... but our PC is in the living room... and the youngest has figured out that if he hits the right keys... he can bring up Internet Explorer....
0
by: ferpino | last post by:
Hello I've a NLB(Network Load Balanced) in Windows 2000 Advanced Server. I want that when a service fail in one of the servers, inmediately all the network flows goes to the other one. I think...
1
by: knopper | last post by:
Hi, Any body know how to disable a network card using c# ? Or to simulate a "cablbe unplugged". So the network card sysmbol shows only a red x, however it a ether is still plugged in. thanks...
2
by: Ari | last post by:
hi all, I try to use the shell32 to enum the network adapters that installed on my computer, it works, but I want to get more details about them, such as the device's type, device's name ..., how...
0
by: Steve D | last post by:
Hi. I am using the following code to try and populate two list boxes. The first, lstWorkgroups, is to show a list of the workgroups on my network. The second, lstComputers, is to show a list of the...
0
by: 4AspNet | last post by:
Hello. I need to disable and then enable Local Network Connection (Ethernet). Here is my code: IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();...
8
by: rongchaua | last post by:
Hi all, i would like now to disable and enable network adapter programmatically with c#. I have searched but found nothing useful. There's no topic about this problem. Has someone done with this...
10
by: Gary Jefferson | last post by:
Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B', and each module has its own logger, created with: module1logger = logging.getLogger('project.A') and module2logger =...
0
by: BLUE | last post by:
Enable and Disable in the Network and DialUp Connections manager is actually setting a bit in the registry for the target adapter and then calling NDIS to unbind or bind (depending on whether your...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.