Microsoft KB Archive/303902

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

Article ID: 303902

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * 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 TASK
SUMMARY
 * Steps to Build the Sample



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.

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(&quot;NetBIOS Name = {0}&quot;, result);

//Retrieve the DNS name. result = System.Net.Dns.GetHostByName(&quot;LocalHost&quot;).HostName;

//Display the results to the console window. Console.WriteLine(&quot;DNS Name = {0}&quot;, result); </li> 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.</li></ol>

back to the top

Additional query words: environment

Keywords: kbhowtomaster kblangc kbprod2web KB303902

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.