How to get Windows Product Name in C# and VB.NET

To get Windows Product Name in C# and VB.NET you can use the following snippet.

Sample C#

public static string GetWindowsProdutName()
{
	var name = (new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>().Select(x => x.GetPropertyValue("Caption"))).First();
	return name != null ? name.ToString() : "Unknown";
}

Sample VB.NET

Public Shared Function GetWindowsProdutName() As String
	Dim name = (New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").[Get]().OfType(Of ManagementObject)().[Select](Function(x) x.GetPropertyValue("Caption"))).First()
	Return If(name IsNot Nothing, name.ToString(), "Unknown")
End Function

Tipp: To use ManagementObjectSearcher you need to set a Reference to System.Management.

How to hide a Console Window in C# and VB.NET

To hide a Console Window in C# and VB.NET you can use the following snippet.

Sample C#

public static class Consoles
{
	[DllImport("user32.dll")]
	public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

	[DllImport("user32.dll")]
	public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

	public static void SetConsoleWindowVisibility(bool visible, IntPtr hWnd)
	{
		ShowWindow(hWnd, !visible ? 0 : 1);
	}
}

usage:

var hWnd = Consoles.FindWindow(null, Console.Title);
Consoles.SetConsoleWindowVisibility(false, hWnd);

Sample VB.NET

Public NotInheritable Class Consoles
	Private Sub New()
	End Sub
	<DllImport("user32.dll")> _
	Public Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
	End Function

	<DllImport("user32.dll")> _
	Public Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
	End Function

	Public Shared Sub SetConsoleWindowVisibility(visible As Boolean, hWnd As IntPtr)
		ShowWindow(hWnd, If(Not visible, 0, 1))
	End Sub
End Class

usage:

Dim hWnd = Consoles.FindWindow(Nothing, Console.Title)
Consoles.SetConsoleWindowVisibility(False, hWnd)

How to reset the windows audio mixer in Batch

To reset the windows audio mixer in Batch you can use the following snippet.
This snippet will set all volume levels equal. If you know a simpler way to reset the audio mixer, without setting all volume levels manually please let us know.

Sample Batch

@ECHO OFF
ECHO Resetting Volume Mixer Settings!
NET STOP Audiosrv
NET STOP AudioEndpointBuilder
REG DELETE "HKCU\Software\Microsoft\Internet Explorer\LowRegistry\Audio\PolicyConfig\PropertyStore" /F
REG ADD "HKCU\Software\Microsoft\Internet Explorer\LowRegistry\Audio\PolicyConfig\PropertyStore"
NET START Audiosrv

How to list all installed windows services in C# and VB.NET

To list installed services in C# and VB.NET you can use the following snippet.

Sample C#

public static void ListInstalledServices()
{
	var services = ServiceController.GetServices();
	foreach (var service in services)
	{
		Console.WriteLine(@"Installed: {0}", service.ServiceName);
	}
}

Sample VB.NET

Public Shared Sub ListInstalledServices()
	Dim services = ServiceController.GetServices()
	For Each service As var In services
		Console.WriteLine("Installed: {0}", service.ServiceName)
	Next
End Sub