Verify Internet connection is available using C#

Standard

While reviewing some code, I found a snippet for checking internet connection, like this

private static bool IsConnectedtoInternet()
{
	bool IsConnectedtoInternet;
	try
	{
		using (WebClient client = new WebClient())
		{
			using (client.OpenRead("http://www.microsoft.com"))
			{
				IsConnectedtoInternet = true;
			}
		}
	}
	catch
	{
		IsConnectedtoInternet = false;
	}
	return IsConnectedtoInternet;
}

I found this way of internet connection availablilty many times, but most of the times it was google.com :) So I thought of writing the correct way (at least from my prespective) of verifying internet connection. For this I am using a WIN32 API, InternetGetConnectedState method, from wininet.dll. And here is the snippet.

[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState
    (out int Description, int ReservedValue);
private static bool IsConnectedtoInternet()
{
    int description;
    return InternetGetConnectedState(out description, 0);
}

Happy Programming :)

CreateObject equivalent for C#

Standard

In current project, I had to use some 3rd party APIs, which is exposed via COM Interop. I found some VB.Net code to consume, but I couldn’t find in C# implementation for the same. Here is the code snippet which is equivalent VB.Net CreateObject method.

var txt = "HelloWorld";
var type = Type.GetTypeFromProgID("vbscript.regexp");
dynamic vbScriptRegEx = Activator.CreateInstance(type);
vbScriptRegEx.Pattern = "l";
Console.WriteLine(vbScriptRegEx.Replace(txt, "##"));

You may get some other errors, if your platform / platform target is different from the COM object compiled platform.

OpenXML and opening a file in Read only mode

Standard

In one of my project, I am using OpenXML SDK for opening Excel files. Recently I got an issue like SpreadsheetDocument.Open() method was throwing an IOException, if the Excel file is opened by MS Excel, even if I set the isEditable parameter false.

IO Exception - Open XML opening file in Read Only mode

IO Exception – Open XML opening file in Read Only mode

I fixed this problem by passing a stream instead of string (file path). And I used the File stream class to open the Excel file. Here is the code snippet.

using (var fileStream = new FileStream(fileName, FileMode.Open, 
    FileAccess.Read, FileShare.ReadWrite))
{
    using (var spreadSheetDocument = SpreadsheetDocument.Open(fileStream, false))
    {
        //Implementation
    }
}

Happy Coding :)

Generate HTML file from XML using XSLT

Standard

Recently I had to work on FxCop, for static code analysis. For those who don’t know what is FxCop, check this link. But one issue I faced was, the report can be saved / exported from FxCop is only in XML format. I searched for tool which help me to convert this XML to any other formats like HTML or doc, but I couldn’t find anything. :( Then I found FxCop comes with some XSL file, which help you to convert the FxCop generated XML to HTML. You can find these XSL files under – “C:\Program Files (x86)\Microsoft Fxcop 10.0\Xml” folder. And here is the code which helps to generate HTML using XML and XSLT.

public static string TransformXMLToHTML(string xmlFile, string xsltFile)
{
    var transform = new XslCompiledTransform();
    using (var reader = XmlReader.Create(File.OpenRead(xsltFile)))
    {
        transform.Load(reader);
    }

    var results = new StringWriter();
    using (var reader = XmlReader.Create(File.OpenRead(xmlFile)))
    {
        transform.Transform(reader, null, results);
    }

    return results.ToString();
}

Happy Programming :)