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.
private extern static bool InternetGetConnectedState
(out int Description, int ReservedValue);
private static bool IsConnectedtoInternet()
return InternetGetConnectedState(out description, 0);
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";
You may get some other errors, if your platform / platform target is different from the COM object compiled platform.
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
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,
using (var spreadSheetDocument = SpreadsheetDocument.Open(fileStream, false))
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)))
var results = new StringWriter();
using (var reader = XmlReader.Create(File.OpenRead(xmlFile)))
transform.Transform(reader, null, results);