Results 1 to 5 of 5

Thread: How do I get the full domain name in VB.NET

  1. #1
    Join Date
    Jan 2010
    Posts
    311

    How do I get the full domain name in VB.NET

    I want a common sign on solution developed in VB.NET for my company's software. For this I require the full domain name of the server. But my problem is that I don't have any idea about how to do so. Someone suggested me to use Environ("USERDNSDOMAIN"), but since I am not an expert in VB.NET I need your guidance on how to use it. Below is my code:

    Code:
    Public Class MyForm Inherits System.Web.UI.Page
    
    #MyCode "My Generated Code"
    
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        End Sub
    
        Protected WithEvents txtDomain As System.Web.UI.WebControls.TextBox
        Protected WithEvents lblName As System.Web.UI.WebControls.Label
        Protected WithEvents btnRefresh As System.Web.UI.WebControls.Button
       
        Private designerPlaceholderDeclaration As System.Object
    
        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            InitializeComponent()
        End Sub
    
    #End MyCode 
    
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            txtDomain.Text = "Test"
        End Sub
    
        Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
            txtDomain.Text = System.Environment.GetEnvironmentVariable("USERDNSDOMAIN")
        End Sub
    
    End Class

  2. #2
    Join Date
    Nov 2008
    Posts
    1,022

    Re: How do I get the full domain name in VB.NET

    You missed out to add system.management reference.

    Code:
    Imports System.Management
    
    Module MyModule 
    
        Sub Main()
            Using objSearch As New ManagementObjectSearcher(New SelectQuery("Win32_ComputerSystem"))
                For Each mngtObj As ManagementObject In searcher.[Get]()
                    Dim myDomainName As String = String.Format("{0}.{1}", mngtObj("name"), If(CBool(mngtObj("partofdomain")), mngtObj("domain"), mngtObj("workgroup")))
                    Console.WriteLine(myDomainName)
                    Exit For
                Next
            End Using
    
        End Sub
    
    End Module

  3. #3
    Join Date
    May 2008
    Posts
    685

    Re: How do I get the full domain name in VB.NET

    A better idea would be:

    Code:
    Public Function GetCompInfo() As Boolean
    
            Dim objSearch As ManagementObjectSearcher
            Dim objCollection As ManagementObjectCollection
            Dim sqlCommand As String = "SELECT * FROM Win32_ComputerSystem"
            Dim mngtScope As ManagementScope = New ManagementScope("root\cimv2")
            Dim newQuery As SelectQuery = New SelectQuery(sqlCommand)
    
            objSearch = New ManagementObjectSearcher(mngtScope, newQuery)
            objCollection = objSearch.Get()
    
            Dim mngtObject As ManagementObject
    
            For Each management_object In objCollection 
                Console.WriteLine(mngtObject("Domain") + "\" + mngtObject("Name"))
            Next mngtObject 
    
            Return True
        End Function

  4. #4
    Join Date
    Jan 2010
    Posts
    311

    Re: How do I get the full domain name in VB.NET

    Thank you for your suggestions. I tried all that changing it based on my software. CitricAcid, I am unable to let my Visual Studio understand the using function but always I end up with build error. However I tried something different as below:

    Code:
    Private Function myDomainName()
        Dim ipHost as System.Net.IPHostEntry = System.Net.Dns.Resolve("SERVER")
        Dim domainName As String = ipHost.hostname
        Return domainName 
    End Function

  5. #5
    Join Date
    Nov 2008
    Posts
    1,054

    Re: How do I get the full domain name in VB.NET

    Here is a slight modification to your code which is required according to me:

    Code:
    Dim getHostName As String = System.Net.Dns.GetHostName
    
    Private Function myDomainName()
        Dim getHostName As String = System.Net.Dns.GetHostName
        Dim ipHost as System.Net.IPHostEntry = System.Net.Dns.Resolve(getHostName)
        Dim domainName As String = ipHost.hostname
        Return domainName 
    End Function

Similar Threads

  1. Replies: 7
    Last Post: 28-12-2010, 09:05 PM
  2. Replies: 5
    Last Post: 24-08-2010, 03:12 AM
  3. Replies: 1
    Last Post: 16-09-2009, 10:11 AM
  4. backup domain controller in a small business server 2003 domain
    By Ashish Goenkar in forum Small Business Server
    Replies: 4
    Last Post: 30-01-2009, 11:16 PM
  5. Replies: 1
    Last Post: 17-09-2008, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •