[英]Test if Crystal Reports for Visual Studio 2010 (and later) is installed on client
我繼承了一個舊的Windows窗體應用程序(除其他外)是使用Crystal報表生成PDF:s進行打印。 我試圖使其使用Crystal Reports for Visual Studio 2010
組件的Crystal Reports for Visual Studio 2010
而不是相同版本的舊版本,但是在檢查客戶端計算機上是否安裝了所需的版本時遇到了問題。 我希望該應用程序即使沒有安裝Crystal也可以正常工作,當然要生成PDF。
該應用程序執行以下操作(不包括無關詳細信息):
try {
ReportDocument report = new ReportDocument();
//do something with the report, produce pdf etc
}
catch(Exception e) {
Log.Write(e);
throw();
}
然后在調用表單中再次捕獲該異常,該調用表單依次顯示一些信息,包括下載正確的Crystal安裝文件的鏈接。
我以為這以前是可行的,但是在我更新到Crystal組件13。#。#。#之后,這已經不夠了。 ReportDocument report = new ReportDocument();
觸發System.TypeInitializationException
將report
保留為null。 因此,我以后無法處理任何事情。 但是,創建了某些東西 ,因為過一會兒, Finalizer
運行,導致引發另一個System.TypeInitializationException
,進而導致應用程序崩潰。
如何在運行new ReportDocument();
之前測試是否已安裝Crystal new ReportDocument();
以某種方式不會導致終結器線程上的后續異常?
嘗試使用反射加載類型。 這應該避免需要實例化,但是將允許您驗證可以創建Type。
決定接受@PhillipH的回答,因為他指出了我的思考方向。 但是我仍然想展示我的實際工作。
我在ReportDocument report = new ReportDocument();
上遇到的異常ReportDocument report = new ReportDocument();
行說(除其他事項外):
Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The system cannot find the file specified.
因此,為了進行測試,我添加了以下內容:
System.Reflection.Assembly a = System.Reflection.Assembly.ReflectionOnlyLoad("CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304");
在更新ReportDocument
之前。 如果未安裝Crystal,則會拋出該異常,調用表單將捕獲該表單並顯示“這里是如何下載和安裝Crystal”表單。
免責聲明 :我不習慣於使用Reflection
因此我不能真正地說出這是好事還是完全愚蠢的事情。 似乎可以工作,但更改后沒有Finalizer
錯誤。
我只是檢查組裝路徑:
If Not Directory.Exists(" C:\Windows\assembly\GAC_MSIL\CrystalDecisions.CrystalReports.Engine") Then
InstallCrystalRuntime()
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.