Things You'll Need:
- Microsoft Visual C# 2008 Express (free)
-
Step 1
Note: This article assumes you have installed Microsoft Visual C# 2008 Express Edition. You may download it for free from here: http://www.microsoft.com/express/download/
Open Microsoft Visual C#. Click on "Project..." to the right of Create in the Recent Projects area of the Start Page.
The New Project window will open. Click on "Windows Forms Application", enter a Name, and click OK.
By default, the only form in the project will be called "Form1" and you will be in Design mode for that form. -
Step 2
Hover over the Toolbox on the left side of the screen and the Toolbox will automatically expand. Click and drag a Button control, under the Common Controls category, onto the form.
-
Step 3
Double click the button and you will now be in the code window for Form1. The method for the button click will already be created.
-
Step 4
Add "using System.Net;" just beneath "using System.Windows.Forms;". This namespace contains the Dns object that has the GetHostName and GetHostEntry methods that we will be using.
-
Step 5
In the button1_Click method, place the following code:
try
{
string MyString = "";
string MyUserName = Environment.UserName;
string MyDomainName = Environment.UserDomainName;
string MyComputerName = Dns.GetHostName();
string MyIPAddress = Dns.GetHostEntry(MyComputerName).AddressList[0].ToString();
MyString = MyString + "MyUserName = " + MyUserName + "\r\n";
MyString = MyString + "MyDomainName = " + MyDomainName + "\r\n";
MyString = MyString + "MyComputerName = " + MyComputerName + "\r\n";
MyString = MyString + "MyIPAddress = " + MyIPAddress + "\r\n";
MessageBox.Show(MyString);
}
catch (Exception MyError)
{
MessageBox.Show("AN error has occurred: " + MyError.Message);
}
The try/catch statements will capture any error that occurs and will pop-up a MessageBox with the error displayed.
We will use the Environment.UserName property to extract the UserName, and the Environment.UserDomainName property to extract the domain name.
The Dns object has two methods we will use: GetHostName for the computer name, and GetHostEntry for the IP address.
GetHostEntry returns an IPHostEntry object, which has an array of IPAddress objects called AddressList. Remember that a computer can have more than one IP address, but for our example, we'll only grab the first element of the array since I know that my machine only has one IP address (Dns.GetHostEntry(MyComputerName).AddressList[0]). Then we use the standard ToString method to extract the human-readable string representing the IP address.
Lastly, we display the information in a MessageBox. Note that I placed a carriage return (\r) and new line (\n) at the end of each concatenation so that the output would be easier to read. -
Step 6
Go up to the toolbar and run the program by clicking on the Start Debugging (f5) play button.
NOTE: If you got any kind of error after clicking the play button, you probably made a syntax error when typing the code. Reread the code until you find and correct the error and try again. -
Step 7
The form will take a moment and then pop up. Awesome!















