Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Created June 11, 2013 16:59
Show Gist options
  • Save JohanLarsson/5758610 to your computer and use it in GitHub Desktop.
Save JohanLarsson/5758610 to your computer and use it in GitHub Desktop.
public class InternetConnection
{
/// <summary>
/// Check if internetconnection is available
/// </summary>
/// <param name="lpdwFlags">Returns info about connection</param>
/// <param name="dwReserved">Reserved pass 0 (default)</param>
/// <returns></returns>
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
private extern static bool InternetGetConnectedState(ref InternetConnectionState_e lpdwFlags, int dwReserved = 0);
/// <summary>
///
/// </summary>
/// <param name="lpszUrl">the url ex "http://www.google.se/"</param>
/// <param name="dwFlags"></param>
/// <param name="dwReserved">Reserved pass 0 (default)</param>
/// <returns></returns>
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetCheckConnection(string lpszUrl, InternetCheckConnectionFlags dwFlags = InternetCheckConnectionFlags.FLAG_ICC_FORCE_CONNECTION, int dwReserved = 0);
public bool CheckConnection(string url)
{
return InternetCheckConnection(url);
}
public InternetConnectionState_e GetConnectedState()
{
InternetConnectionState_e lpdwFlags = (InternetConnectionState_e) 0;
InternetConnection.InternetGetConnectedState(ref lpdwFlags);
return lpdwFlags;
}
}
[Flags]
public enum InternetCheckConnectionFlags : int
{
FLAG_ICC_FORCE_CONNECTION = 0x1
}
[Flags]
public enum InternetConnectionState_e : int
{
INTERNET_CONNECTION_MODEM = 0x1,
INTERNET_CONNECTION_LAN = 0x2,
INTERNET_CONNECTION_PROXY = 0x4,
INTERNET_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}
public class Tests
{
[TestCase(@"https://www.google.com/", true)]
[TestCase(@"https://www.akdsfgiayebcvi.com/",false)]
public void CheckConnectionTest(string url, bool expected)
{
var actual = new InternetConnection().CheckConnection(url);
Assert.AreEqual(expected,actual);
}
[Test]
public void TestNameTest()
{
var internetConnectionStateE = new InternetConnection().GetConnectedState();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment