[英]Check if Adobe Acrobat Reader installed - pdf in WebBrowser control
我有一個應用程序,它使用以下代碼在 webBrowser 控件中顯示 pdf
webBrowser1.Navigate(filename + "#toolbar=0");
如果安裝了 Adobe Reader 就可以完美運行
我想在顯示 window 之前或至少在嘗試顯示 pdf 時檢查是否安裝了 Adobe Acrobat Reader。
我已經從這里調整了以下代碼檢查 Adobe Reader 是否已安裝(C#)?
正如評論中提到的,不幸的是,它也標記了已卸載的版本。 我也在同一篇文章中嘗試了 64 位代碼,但發現我無法輕松解決的錯誤,並且懷疑無論如何都會給出相同的結果,因為它只是以類似的方式查看注冊表。
using System;
using Microsoft.Win32;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Adobe");
if(null == adobe)
{
var policies = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Policies");
if (null == policies)
return;
adobe = policies.OpenSubKey("Adobe");
}
if (adobe != null)
{
RegistryKey acroRead = adobe.OpenSubKey("Acrobat Reader");
if (acroRead != null)
{
string[] acroReadVersions = acroRead.GetSubKeyNames();
Console.WriteLine("The following version(s) of Acrobat Reader are installed: ");
foreach (string versionNumber in acroReadVersions)
{
Console.WriteLine(versionNumber);
}
}
}
}
}
}
如果未加載 Adobe pdf 閱讀器,則會出現打開提示(在任何其他已安裝的閱讀器中),保存文件或取消。 我希望能夠截獲此內容,以表明 Adobe 的閱讀器不可用。 我努力了
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
string url = e.Url.ToString();
if (url.StartsWith("res://ieframe.dll/navcancl.htm") && url.EndsWith("pdf"))
{
e.Cancel = true;
MessageBox.Show("Cannot open PDF!");
}
}
在以下https://social.msdn.microsoft.com/Forums/en-US/46aaeecd-5317-462a-ac36-9ebb30ba90e7/load-pdf-file-using-webbrowser-control-in-windows-form-c ?forum=csharpgeneral但發現打開、保存取消事件在 webBrowser1_Navigating 之前。
對於不會標記已卸載版本的可靠解決方案或將停止打開、保存、取消提示並允許我創建消息以安裝閱讀器的單獨解決方案的任何幫助,我將不勝感激
謝謝
簡單的方法是在 try 中使用 try / catch 給出代碼。 如果出現錯誤,web 瀏覽器會報錯,所以在 catch 中說用戶安裝 adobe reader。 這是示例
try
{
//your code goes her
}
catch(Exception ex)
{
if(ex.Message != null)
{
Console.WriteLine("Adobe Reader not Installed");
//this is where you say adobe reader is not installed
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.