How to obtain the NetBIOS and DNS computer names by using Visual Basic .NET or Visual Basic 2005

For a Microsoft Visual C# .NET version of this article, see
303902 .
For a Microsoft Visual Basic 6.0 version of this article, see
175472 .

IN THIS TASK

Summary

This article demonstrates how to obtain the name of the computer on which your application is running. The sample code in this article demonstrates how to obtain both the NetBIOS (simple) name and the fully qualified Domain Name System (DNS) computer name.

Steps to Build the Sample

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. Under Project Types, click Visual Basic Projects. Under Templates, click Console Application.

    Note In Visual Studio 2005, click Visual Basic under Other Languages. Under Templates, click Console Application.
  3. Double-click Module1.vb to open the code window.

    NOTE: You can also right-click Module1.vb, and then click View Code to open the code window.
  4. Add the following code to Sub Main:
    Dim result As String

    '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)
    Note In Visual Studio 2005, you can change the code "System.Net.Dns.GetHostByName" to "System.Net.Dns.GetHostEntry" in line 13. For more information, visit the following Microsoft Web site:
  5. Press the CTRL+F5 key combination to build and run the application. The NetBIOS and DNS computer names appear in the console window.

    NOTE: If you use the CTRL+F5 key combination to start the program, the console window remains open after the code has completed.
Propriedades

ID do Artigo: 302656 - Última Revisão: 6 de dez de 2006 - Revisão: 1

Comentários