How to get current Domain of the PC in C# and VB.NET

To get current Domain of the PC in C# and VB.NET you can use the snippet below.

Sample C#

using System;
using System.DirectoryServices.ActiveDirectory;

namespace de.Fesslersoft.DomainTest
    class Program
        static void Main(string[] args)
            var myDomain = Domain.GetComputerDomain();
            foreach (DomainController dc in myDomain.DomainControllers)
                Console.WriteLine("{0} - {1}", dc.IPAddress, dc.Name);

Sample VB.NET

Imports System.DirectoryServices.ActiveDirectory

Namespace de.Fesslersoft.DomainTest
	Class Program
		Private Shared Sub Main(args As String())
			Dim myDomain = Domain.GetComputerDomain()
			For Each dc As DomainController In myDomain.DomainControllers
				Console.WriteLine("{0} - {1}", dc.IPAddress, dc.Name)
		End Sub
	End Class
End Namespace

Please rate this snippet