How to obtain the NetBIOS and DNS computer names by using Visual C#

Article translations Article translations
Article ID: 303902 - View products that this article applies to.
This article was previously published under Q303902
Expand all | Collapse all

On This Page

SUMMARY

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.

Steps to Build the Sample

  1. Start Microsoft Visual Studio.
  2. Under Project Types, click C# Projects. Under Templates, click Console Application.

    Note In Visual Studio 2005 or in Visual Studio 2008, click Visual C# under Project Types.
  3. 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 or in Visual Studio 2008, the default file is Program.cs. You can double-click Program.cs to open the code window.
  4. 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);
    					
  5. 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.

Properties

Article ID: 303902 - Last Review: July 14, 2008 - Revision: 3.0
APPLIES TO
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# 2008 Express Edition
Keywords: 
kbhowtomaster kblangc kbprod2web KB303902

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com