Suman Ghosh

Hello Experts,

How can I edit DNS and ALT DNS of my LAN card [TCP/IP Properties] through a program written in C# 2.0

I have one Admin User and one General User in my computer running on Windows XP. I cannot access the network properties from General User's login.

I want to write a program which should run from General User's login and simply provide the option to modify the DNS and ALt DNS of the LAN Card.

Can anyone help me on how to do it I suppose I need to use System.net namespace

Thanks/Regards,
Suman Ghosh



Re: .NET Framework Networking and Communication How to edit DNS and ALT DNS of LAN Card

Arnshea Clayton

I'm not sure exactly but 2 things come to mind:

1-Using netsh. Open the netsh process from C# if you have to run it from within the executable.

2-WMI. There may be a WMI class with properties (or methods) you can set to change this.

- Looks like the WMI class you need is Win32_NetworkAdapterConfiguration (the DNSServerSearchOrder[]).

This doesn't address the permissions problem - you may need to provide this as a service that runs in a privileged account.

After looking around in the documentation, it looks like there is a tool that will save you (and me, I use WMI a lot!) a lot of work. mgmtclassgen.exe will generate strongly typed classes for WMI objects. So, open up a visual studio command prompt and type the following:

mgmtclassgen Win32_NetworkAdapter /P Win32_NetworkAdapter.cs

mgmtclassgen Win32_NetworkAdapterConfiguration /P Win32_NetworkAdapterConfiguration.cs

mgmtclassgen Win32_NetworkAdapterSetting /P Win32_NetworkAdapterSettings.cs

Create a console project then include those .cs files in the project. A code sample is below.

You'll find documentation on the WMI classes in the WMI docs (in my version of the visual studio help they're in

MSDN Library

Setup and System Administration

Windows Management Instrumentation

SDK Documentation

WMI Reference

Win32 Classes

Computer System Hardware Classes

Code Snippet

using System;

using System.Management;

using ROOT.CIMV2.Win32; // the namespace used by the generated files

static void Main(string[] args)

{

foreach (NetworkAdapterSetting setting in NetworkAdapterSetting.GetInstances())

{

NetworkAdapter adapter = new NetworkAdapter(setting.Element);

NetworkAdapterConfiguration config = new NetworkAdapterConfiguration(setting.Setting);

Console.WriteLine("adapter name={0}, DHCPEnabled ={1}", adapter.Name, config.DHCPEnabled);

}

}






Re: .NET Framework Networking and Communication How to edit DNS and ALT DNS of LAN Card

Suman Ghosh

Hi Clayton,

Thanks for your response.

I know it can be done using SetDNSServerSearchOrder method, but dont know how to implement the method. Can you please help me with that

DNSServerSearchOrder returns the list of DNS server IPs, I could do that, but I am stuck at how to modify it.

Please help.

Regards,
Suman Ghosh





Re: .NET Framework Networking and Communication How to edit DNS and ALT DNS of LAN Card

Suman Ghosh

Hi Clayton,

I know very very little about WMI... I don't work with it, but just felt curious and started working with it playfully.

I have a question, in my project I have included System.Management namespace, do I still need to use the files that were rendered by the following commands:

mgmtclassgen Win32_NetworkAdapter /P Win32_NetworkAdapter.cs

mgmtclassgen Win32_NetworkAdapterConfiguration /P Win32_NetworkAdapterConfiguration.cs

mgmtclassgen Win32_NetworkAdapterSetting /P Win32_NetworkAdapterSettings.cs


since I feel those are the classes which are already included within the System.Management namespace.

Thanks in advance.

Suman





Re: .NET Framework Networking and Communication How to edit DNS and ALT DNS of LAN Card

Suman Ghosh

Hi Clayton,

I don't have enough word to thank you, thanks for the help. Getting those CS files helped me a lot on how to implement them.

I am pasting the code below for everyone who might feel this would help them:

We need to include the following namespaces
1) System
2) System.ComponentModel
3) System.Windows.Forms
4) System.Management

//Modify IP Address, Subnet Mask, Default Gateway, DNS and ALternate DNS
public void setIP(string IPAddress, string SubnetMask, string Gateway, string DNS, string ALTDNS)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterCon
figuration");
ManagementObjectCollection objMOC = objMC.GetInstances();

foreach (ManagementObject objMO in objMOC)
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
ManagementBaseObject objNewGate = null;
ManagementBaseObject objNewDNS = null;
ManagementBaseObject objSetDNS = null;

if (!(bool)objMO["IPEnabled"])
continue;

try
{


objNewIP = objMO.GetMethodParameters("EnableStatic"
);
objNewGate = objMO.GetMethodParameters("SetGateways")
;
objNewDNS = objMO.GetMethodParameters("SetDNSServerS
earchOrder");

//Set DefaultGateway
objNewGate["DefaultIPGateway"] = new string[] { Gateway };
objNewGate["GatewayCostMetric"] = new int[] { 1 };

//Set IPAddress and Subnet Mask
objNewIP["IPAddress"] = new string[] { IPAddress };
objNewIP["SubnetMask"] = new string[] { SubnetMask };

objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, null);

objNewDNS["DNSServerSearchOrder"] = new string[] { DNS, ALTDNS };
objSetDNS = objMO.InvokeMethod("SetDNSServerSearchOr
der", objNewDNS, null);

Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!");
}
catch (Exception ex)
{
MessageBox.Show("Unable to Set IP : " + ex.Message);
}
finally
{
objNewIP = null;
objSetIP = null;
objNewGate = null;
objNewDNS = null;
objSetDNS = null;
}
}
}


Regards,
Suman Ghosh





Re: .NET Framework Networking and Communication How to edit DNS and ALT DNS of LAN Card

Arnshea Clayton

No problem. You can also use the generated classes directly - so you can call the NetworkAdapterConfiguration.SetDNSServerSearchOrder(). You'll have to include the generated classes in your project.






Re: .NET Framework Networking and Communication How to edit DNS and ALT DNS of LAN Card

Suman Ghosh

Hi Clayton,

Is there anyway to run this small program from under a normal user's [non-admin, non-power user] login Right now it works properly under admin user but doesn't work under normal user although it doesn't generate any error.

Please help.

Regards,
Suman Ghosh