How to download a file using Python

To download a file using Python you can use the snippet below.

Sample Python

import urllib.request
import shutil

# file_name = target file name
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
    shutil.copyfileobj(response, out_file)

Please rate this snippet

How to open a Website in the default Browser in C# and VB.NET

To open a Website in the default Browser in C# and VB.NET you can use the snippet below.

Sample C#

System.Diagnostics.Process.Start("http://codesnippets.fesslersoft.de/");

Sample VB

System.Diagnostics.Process.Start("http://codesnippets.fesslersoft.de/")

Please rate this snippet

How to ping in Java

To ping in Java you can use the following snippet.

Sample Java

public static void pingUrl(String url, Integer timeOut)
{
	url = url.replace("http://", "");
	try {  
		InetAddress address = InetAddress.getByName(url);  
		System.out.println("Hostname: " + address.getHostName());  
		System.out.println("IP-Address: " + address.getHostAddress());  
		System.out.println("Reachable in " + ((double)timeOut / (double)1000) + " Seconds: " + address.isReachable(timeOut));  
	}  
	catch (UnknownHostException e) {
		System.err.println("Unable to lookup " + url + "; error: " + e);  
	}  
	catch (IOException e) {  
		System.err.println("Unable to reach " + url);  
	}  
}

Please rate this snippet

How to download a file in Powershell

To download a file in Powershell you can use the following snippet.

Sample Powershell

(New-Object System.Net.WebClient).DownloadFile('http://download.tuxfamily.org/notepadplus/6.6.6/npp.6.6.6.Installer.exe', 'C:\Users\MF\Codesnippets\Desktop\Download\npp.6.6.6.Installer.exe')

Please rate this snippet

How to check if internet is avaiable in C# and VB.NET

To check if internet is avaiable in C# and VB.NET you can use the following snippet.

Sample C#

private static bool IsInternetAvaiable(string url)
{
	try
	{
		using (var client = new WebClient())
		using (var stream = client.OpenRead(url))
		{
			return true;
		}
	}
	catch (Exception ex)
	{
		//catch and handle the exception your way
		return false;
	}
}

Sample VB.NET

Private Shared Function IsInternetAvaiable(ByVal url As String) As Boolean
	Try 
		Dim client As var = New WebClient
		Dim stream As var = client.OpenRead(url)
		Return true
	Catch ex As Exception
		'catch and handle the exception your way
		Return false
	End Try
End Function

Please rate this snippet