|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Introduction
BackgroundThe main idea of this application is changing network settings via WMI, I access WMI by using Using the codeThere is a method which returns the list of available connections. This list is filled from the VMI provider. public List<Connection> GetConnections()
{
List<Connection> insListConnection = new List<Connection>();
ManagementObjectSearcher m = new ManagementObjectSearcher();
m.Query = new ObjectQuery
("Select * from Win32_NetworkAdapterConfiguration
Where IPEnabled = True");
foreach (ManagementObject mo in m.Get())
{
Connection c = new Connection();
c.ConnectionId = mo["Index"].ToString();
c.ConnectionName = mo["Caption"].ToString().Substring
(mo["Caption"].ToString().IndexOf("]") + 1).Trim();
insListConnection.Add(c);
}
return insListConnection;
}
After creating an instance and filling the connection and profile object by passing these objects as parameter to the method named public void SetConnectionProfile(Connection c, Profile p)
{
ManagementObjectSearcher m = new ManagementObjectSearcher();
m.Query = new ObjectQuery("Select *
from Win32_NetworkAdapterConfiguration Where IPEnabled = True");
foreach (ManagementObject mo in m.Get())
{
if (mo["Index"].ToString()==c.ConnectionId)
{
mo.InvokeMethod("EnableStatic", new object[] {new string[]
{ p.IpAddress }, new string[] { p.SubnetMask } });
mo.InvokeMethod("SetGateways", new object[] {new string[]
{ p.DefaultGateway }, new string[] { "1" } });
mo.InvokeMethod("SetDNSServerSearchOrder", new object[]
{new string[] { p.PreferredDns} });
if (p.ProxyEnabled)
{
RegistryManager.EnableProxy = true;
RegistryManager.BypassProxyForLocalAddresses =
p.ByPassProxyForLocal;
RegistryManager.ProxyAddress= p.ProxyAddress + ":" +
p.ProxyPort;
}
else
{
RegistryManager.EnableProxy = false;
RegistryManager.ProxyAddress = "";
RegistryManager.BypassProxyForLocalAddresses =true;
}
}
}
}
And there is another method named public void SetConnectionAutomatic(Connection c)
{
ManagementObjectSearcher m =new ManagementObjectSearcher();
m.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration
Where IPEnabled = True");
foreach (ManagementObject mo in m.Get())
{
if (mo["Index"].ToString() == c.ConnectionId)
{
mo.InvokeMethod("EnableDHCP",null);
mo.InvokeMethod("SetDNSServerSearchOrder",null);
RegistryManager.EnableProxy = false;
RegistryManager.ProxyAddress = "";
RegistryManager.BypassProxyForLocalAddresses = true;
}
}
}
Contact
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||