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);
            }
            Console.Read();
        }
    }
}

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)
			Next
			Console.Read()
		End Sub
	End Class
End Namespace

Please rate this snippet

How to Log off, Restart or Shutdown Windows using Batch

To Logoff, Restart or Shutdown Windows using Batch you can Simply execute shutdown with the needed parameters. To execute the shutdown command you need the sufficient user privileges.

List of Parameters

  • -l Logs off the current user, this is also the default. -m ComputerName takes precedence.
  • -s Shuts down the local computer.
  • -r Reboots after shutdown.
  • -a Aborts shutdown. Ignores other parameters, except -l and ComputerName. You can only use -a during the time-out period.
  • -f Forces running applications to close. Note that if you are on a remote machine without using -f your session maybe closed but a hanging program blocks the Shutdown
  • -m [ \\ ComputerName ] Specifies the computer that you want to shut down.
  • -t xx Sets the timer for system shutdown in xx seconds. The default is 20 seconds.
  • -c ” message “ Specifies a message to be displayed in the Message area of the System Shutdown window. You can use a maximum of 127 characters. You must enclose the message in quotation marks.
  • -d [ u ][ p ] : xx : yy Lists the reason code for the shutdown. Reason Codes (u=Indicates a user code), (p=Indicates a planned shutdown cod), (xx=Specifies the major reason code (0-255)), (yy=Specifies the minor reason code (0-65536)
  • /? Displays help at the command prompt.
  • FOR MORE INFORMATIONS AND DETAILS TAKE A LOOK AT THE MSDN.

    Please rate this snippet

    How to get free disk space in C# and VB.NET

    To get free disk space in C# and VB.NET you can use the following snippet.

    Sample C#

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes,out ulong lpTotalNumberOfFreeBytes);
    
    public static bool GetFreeBytes(string folderName, out ulong freeBytes)
    {
    	freeBytes = 0;
    	if (!folderName.EndsWith("\\"))
    	{
    		folderName += '\\';
    	}
    	ulong freeSpaceInBytes = 0;
    	ulong notUsed = 0;
    	ulong notUsed2 = 0;
    	if (GetDiskFreeSpaceEx(folderName, out freeSpaceInBytes, out notUsed, out notUsed2))
    	{
    		freeBytes = freeSpaceInBytes;
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    

    Sample VB.NET

    <DllImport("kernel32.dll", SetLastError := True, CharSet := CharSet.Auto)> _
    Public Shared Function GetDiskFreeSpaceEx(lpDirectoryName As String, ByRef lpFreeBytesAvailable As ULong, ByRef lpTotalNumberOfBytes As ULong, ByRef lpTotalNumberOfFreeBytes As ULong) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    
    Public Shared Function GetFreeBytes(folderName As String, ByRef freeBytes As ULong) As Boolean
    	freeBytes = 0
    	If Not folderName.EndsWith("\") Then
    		folderName += "\"
    	End If
    	Dim freeSpaceInBytes As ULong = 0
    	Dim notUsed As ULong = 0
    	Dim notUsed2 As ULong = 0
    	If GetDiskFreeSpaceEx(folderName, freeSpaceInBytes, notUsed, notUsed2) Then
    		freeBytes = freeSpaceInBytes
    		Return True
    	Else
    		Return False
    	End If
    End Function
    

    Please rate this snippet