Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-05-2010
Member
 
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

Reply With Quote
  #2  
Old 13-05-2010
Member
 
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
Reply With Quote
  #3  
Old 13-05-2010
Member
 
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
Reply With Quote
  #4  
Old 13-05-2010
Member
 
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
Reply With Quote
  #5  
Old 13-05-2010
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How do I get the full domain name in VB.NET"
Thread Thread Starter Forum Replies Last Post
An active directory Domain Controller for the domain could not becontacted in windows vista Business CheyeNnne Active Directory 7 28-12-2010 09:05 PM
Unable to see a forest trusted domain from XP admin tools but able to see from a Domain Controller Assasin boy Networking & Security 5 24-08-2010 03:12 AM
I have got two domain, I can't ping FQDN, Domain name but not the hostname of the other domain kaikoa Networking & Security 1 16-09-2009 10:11 AM
backup domain controller in a small business server 2003 domain Ashish Goenkar Small Business Server 4 30-01-2009 11:16 PM
Canon EOS 5D Mark II 21.1 megapixel with Full-Frame, Full-HD video JamesMK Portable Devices 1 17-09-2008 01:11 PM


All times are GMT +5.5. The time now is 12:28 PM.