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