How to obtain the NetBIOS and DNS computer names by using Visual C# (303902)
The information in this article applies to:
- Microsoft Visual C# .NET (2002)
- Microsoft Visual C# 2005
This article was previously published under Q303902 For a Microsoft Visual Basic .NET version of this article, see 302656.
IN THIS TASKSUMMARY
This step-by-step article shows you how to get the name of the computer on which your application is running. The following sample code shows how to get both the NetBIOS (simple) name and the fully qualified Domain Name System (DNS) computer name.
back to the top
Steps to Build the Sample- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- Under Project Types, click C# Projects. Under Templates, click Console Application.
Note In Visual Studio 2005, click Visual C# under Project Types. - Double-click Class1.cs to open the code window.
Notes- You can also right-click Class1.cs and then click View code to open the code window.
- In Visual Studio 2005, the default file is Program.cs. You can double-click Program.cs to open the code window.
- Paste the following code in Main:
string result;
//Retrieve the NetBIOS name.
result = System.Environment.MachineName;
//Display the results to the console window.
Console.WriteLine("NetBIOS Name = {0}", result);
//Retrieve the DNS name.
result = System.Net.Dns.GetHostByName("LocalHost").HostName;
//Display the results to the console window.
Console.WriteLine("DNS Name = {0}", result);
- Press the CTRL+F5 key combination to build and run the application. The NetBIOS and DNS computer names are displayed in the console window.
NOTE: If you press CTRL+F5 to start the program, the console window remains open after the code has completed.
back to the top
Modification Type: | Minor | Last Reviewed: | 10/4/2006 |
---|
Keywords: | kbHOWTOmaster kbLangC kbProd2Web KB303902 kbAudDeveloper |
---|
|