Enable and disable network adapter using Shell32.dll

E

Eran.Yasso

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.ssfCONTROLS);
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");
}
}
}
}
 
R

Rajendra Gola

Hi,

I am unable to understand your code.

I want a program that can enable or disable network connection on a system.

Would you add comments in your code so that i can understand.


Thanks & Regards

Rajendra
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.ssfCONTROLS);
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");
}
}
}
}

Submitted via EggHeadCafe - Software Developer Portal of Choice
ASP.NET HttpPostedFile Image Resizer
http://www.eggheadcafe.com/tutorial...8778/aspnet-httppostedfile-image-resizer.aspx
 
R

Rajendra Gola

Hi,

I am unable to understand your code.

I want a program that can enable or disable network connection on a system.

Would you add comments in your code so that i can understand.


Thanks & Regards

Rajendra
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.ssfCONTROLS);
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");
}
}
}
}
On Tuesday, October 12, 2010 2:18 AM Rajendra Gola wrote:
Hi,



I am unable to understand your code.



I want a program that can enable or disable network connection on a system.



Would you add comments in your code so that i can understand.





Thanks & Regards



Rajendra
Submitted via EggHeadCafe - Software Developer Portal of Choice
Lucene.Net Indexing Searching Entry Level Tutorial
http://www.eggheadcafe.com/tutorial...-indexing-searching-entry-level-tutorial.aspx
 
Top