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 scroll to the end of a TreeView in C# and VB.NET

To scroll to the end of a TreeView in C# and VB.NET you can use the following snippet.

Sample VB.NET / C#

treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible();

How to scroll to the end of a listview in C# and VB.NET

To scroll to the end of a listview in C# and VB.NET you can use the following snippet.

Sample C# / VB.NET

listView1.EnsureVisible(listView1.Items.Count - 1);

How to make a seperator line in C# and VB.NET

To make a seperator line in C# and VB.NET you can use the following snippet.

Sample C# / VB.NET

label1.Height = 2;
label1.AutoSize = false;
label1.BorderStyle = BorderStyle.Fixed3D;

Result:

SeperatorLine_NET

A Seperator line in .NET

How to start a process and wait for exit in Powershell

To start a application and wait for exit in Powershell you can use the following snippet.

Sample Powershell

start-process -filepath "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" -Wait -NoNewWindow -argumentlist '-args'

for more informations take a look at the MSDN Start-Process

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#

[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 show, IntPtr hWnd)
{
	try
	{
		ShowWindow(hWnd, !show ? 0 : 1);
	}
	catch (Exception ex)
	{
		//handle the exception your way
	}
}

Example usage

var hWnd = Window.FindWindow(null, Console.Title);
if (hWnd != IntPtr.Zero) 
{
	SetConsoleWindowVisibility(showWindows, hWnd);
}

Sample VB.NET

<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(show As Boolean, hWnd As IntPtr)
	Try
		ShowWindow(hWnd, If(Not show, 0, 1))
	Catch ex As Exception
        'handle the exception your way
	End Try
End Sub

Example usage

Dim hWnd = Window.FindWindow(Nothing, Console.Title)
If hWnd <> IntPtr.Zero Then
	SetConsoleWindowVisibility(showWindows, hWnd)
End If

How to do a HTTP redirect using Javascript or jQuery

To do a HTTP redirect using Javascript or jQuery you can use the window.location.href or the window.location.replace property.

Sample Javascript href

window.location.replace("http://codesnippets.fesslersoft.de");

Sample Javascript replace

window.location.href = "http://codesnippets.fesslersoft.de";

difference of these two

you should use replace if you want to create a http redirect and href if you want to simulate a click.

 

You can also do this in jQuery but i personally prefer the methods shown above because there is no need to use jQuery for a http redirect.

Sample jQuery

var newUrl = "http://codesnippets.fesslersoft.de";    
$(location).attr('href',newUrl);

for more information see W3Schools: Location href Property, Location replace() Method