Show domain in windows form C#

deca300

Well-Known Member
Joined
Jan 28, 2010
Messages
138
Reaction score
0
Hi guys

I have found this bit of code on Google:

IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
tboxDNSDomain.Text = properties.DomainName;

This is the output that I am looking for:
domain.jpg



I want to display the domain that the PC is currently running on.
Any help much appreciated.
 
Thats it, just as you execute the exe it should display the domain name.
 
So what's your actual question? You want someone to write the application for you? Why not just check the computer's properties to see which domain they're on?
 
The code looks fine does your pc currently belong to a domain?
 
The code looks fine does your pc currently belong to a domain?

Well, he doesn't need to create an instance of the object like that, instead he can just do the following, which is a bit neater:
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName
 
I'm not on this level in c# So I am not sure how to make it show the IP on a windows form.
 
I'm not on this level in c# So I am not sure how to make it show the IP on a windows form.

The domain isn't the IP... you want the IP to be displayed?

If you want the domain to be displayed on in a textbox called tboxDNSDomain, the code you posted originally is fine. If you want it to look like the forms app you showed, you'll want to use a label rather than a textbox.
 
Last edited:
No I have the IP.

I want to display the domain name.

This is what I have:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Host Computer: " + Dns.GetHostName();
foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
label2.Text = "IP Adress: " + adress;
}
}

private void label1_Click(object sender, EventArgs e)
{

}

private void label2_Click(object sender, EventArgs e)
{

}


Now I want to add a bit just to show the domain name as well.
 
Nice Find deca300.

using Messugga's suggestion to not instantiate an instance is correct.

i would personally use the Using section and define
Code:
using System.Net.NetworkInformation;
and then only access
Code:
string domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName;
like that . Constantly working with the Full namespace is messy.
 
Okay, so add another label to your form. Call it lblDomain or something along those lines.

Then in your Form1_Load method, add the following:
lblDomain.Text = System.Net.NetworkInformation.IPGlobalProperties.G etIPGlobalProperties().DomainName;

By the way, your label2.Text is going to get rewritten for each IP on that machine, so if you have multiple IPs, only the last one returned by the foreach will be displayed.
 
Agreed. But if it's a once-off use of said namespace, it's a bit either/or, in my opinion.

True that, but knowing the useful methods that are available in that namespace especially when it comes to applications build to use it Properly it is a given to map it.
 
just being pedantic, but

Dog dog = DogFactory.getDog();
dog.bark();


is exactly the same as

DogFactory.getDog().bark();

neither have more or less instance creation going on
 
Last edited:
No I have the IP.

I want to display the domain name.

This is what I have:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Host Computer: " + Dns.GetHostName();
foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
label2.Text = "IP Adress: " + adress;
}
}

private void label1_Click(object sender, EventArgs e)
{

}

private void label2_Click(object sender, EventArgs e)
{

}


Now I want to add a bit just to show the domain name as well.

Just a note regarding the Foreach statement , remember using a Label component for that will only five you the last IP Address in the List. here is a bit of code bound to a listbox that you can replace that with to see all IP Addresses associated with your Computer.
Code:
   foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                listBox1.Items.Add ("IP Adress: " + adress);
            }

label3.Text =  IPGlobalProperties.GetIPGlobalProperties().DomainName;
Note that the DomainName will only bring up what is available to it. example being that if your on a Corporate/"educational";) Network ( you have a AD running and your PC is bound to it ) you will get that domain Name. most cases working on a Standalone PC with no domain will give you Workgroup or something like that.
 
Just want to say thank you to all you guys input! I have managed to do this!
 
Top
Sign up to the MyBroadband newsletter
X