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

This article was previously published under Q303902
For a Microsoft Visual Basic .NET version of this article, see 302656.


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

  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.

    • 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.
back to the top

Αναγνωριστικό άρθρου: 303902 - Τελευταία αναθεώρηση: 07/14/2008 23:28:43 - Αναθεώρηση: 3.0

Microsoft Visual C# .NET 2002 Standard Edition, Microsoft Visual C# 2005, Microsoft Visual C# 2008 Express Edition

  • kbhowtomaster kblangc kbprod2web KB303902