Recently I was trying to use a Regular Expression in .NET. I was working with small blobs of text. I was trying to extract some code snippets out of some wiki markup.

The code snippets looked like:

{code:csharp|title=C#}
//C# Code
{code}
{code:vbnet|title=vb.net}
//VB.NET Code
{code}

For testing purposes I was using the simplest Regex I could think of {code:(.+?){code}. After playing around it became obvious that it was not matching over multiple lines. This makes sense because the ‘ .’ character  matches every character except /n/r

Well it turns out you want to enable SingleLineMode not MultiMode.  I know I know makes a ton of sense.

Lesson learned. Read the documentation.

Extra Bonus: If you want to quickly test .NET regular expressions with all these options. I found I like http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

Recently I was working on ClickOnce Application and I was trying to distribute XULRunner with my application. Unfortunately when deploying the application I realized that not all the files where included in the ClickOnce applications file, particularly text files such as files that ended in .ini or .manifest.

The solution was to go into the solution explorer in Visual Studio, select the files that there not being copied, and set the build action to content for those files.

This one has caused me a few hours of pain….a couple of times.  It is about time I document how I fixed it.

The Error:

Cannot import the following key file: <filename>.pfx. The key file may be password protected. To correct this, try to import the certificate again or manually install the certificate to the Strong Name CSP with the following key container name: VS_KEY_ E2AEBF22AB52DD08    <Application Name>

The Fix:

  1. Open Visual Studio Command Prompt (It can be found in the Windows Start menu)
  2. Type sn -i “c:\Pathtofile\<filename>.pfx” TVS_KEY_ E2AEBF22AB52DD08
  3. Reimport the pfx file into Visual Studio

The sn.exe with the –i parameter, installs a key pair from <infile> into a key container named <container>.

This method is a modified version of Mike Borozdin method which I happen to enjoy very much. The biggest changes I made where to add using statements around the disposable objects such as the Bitmap and the Graphics object to avoid memory leaks, as well as a few minor changes.

//Image Resize Helper Method
private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
{
    using (Image originalImage = Image.FromFile(filename))
    {
        //Caluate new Size
        int newWidth = originalImage.Width;
        int newHeight = originalImage.Height;
        double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;

        if (aspectRatio <= 1 && originalImage.Width > maxWidth)
        {
            newWidth = maxWidth;
            newHeight = (int)Math.Round(newWidth / aspectRatio);
        }
        else if (aspectRatio > 1 && originalImage.Height > maxHeight)
        {
            newHeight = maxHeight;
            newWidth = (int)Math.Round(newHeight * aspectRatio);
        }

        Bitmap newImage = new Bitmap(newWidth, newHeight);

        using (Graphics g = Graphics.FromImage(newImage))
        {
            //--Quality Settings Adjust to fit your application
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
            return newImage;
        }
    }
}

The other day, one of our customers was switching over to 64-bit development machines, and encountered the following issue:

System.BadImageFormatException: Could not load file or assembly'(assembly name)' or one of its dependencies. An attempt was made to load a program with an incorrect format.

The likely reason is that you (or in this instance, our customer) are trying to load a 32-bit assembly into a 64-bit application. This can happen when you have the Any CPU platform selected.

Solution:

If your scenario sounds like the one I described, try setting your application to run in x86 mode. In Visual Studio this can be done by going to Build -> Configuration Manager…
Visual Studio Configuration Manager
…and setting the Platform to be x86.
Visual Studio Configuration Manager

This article and many others like this can be found on my SoftArtisans blog.