Search code examples
c#winformscrystal-reports

Test if Crystal Reports for Visual Studio 2010 (and later) is installed on client


I have inherited an old Windows forms application which is (among other things) using Crystal reports to produce PDF:s for printing. I'm trying to make it use Crystal Reports for Visual Studio 2010 components instead of older versions of the same but I'm having problems checking if the required version is installed on the client computer. I want the application to work even if Crystal is not installed, with the exception of PDF generation of course.

The application does something similar to this (irrelevant details excluded):

try {
    ReportDocument report = new ReportDocument();

    //do something with the report, produce pdf etc
}
catch(Exception e) {
    Log.Write(e);
    throw();
}

The exception is then caught again in the calling form which in turn shows some info including a link to download the correct Crystal installation files.

I assume this has been working before, but after I have updated to Crystal components 13.#.#.# this is no longer enough. The ReportDocument report = new ReportDocument(); triggers a System.TypeInitializationException leaving report as null. So there is nothing that I can subsequently dispose. However, something gets created, because a while later, the Finalizer runs, causing another System.TypeInitializationException to be thrown which in turn causes the application to crash.

How can I test if Crystal is installed prior to running new ReportDocument(); in a way that will not cause a subsequent exception on the Finalizer thread?


Solution

  • Try to load the Type using Reflection. This should prevent the need to instantiate but will allow you to verify the Type can be created.