webbrowser控制问题,清除缓存不起作用?

时间:2013-03-04 08:06:00

标签: c# pdf webbrowser-control clear-cache

我正在使用webbrowsercontrol来显示本地存储的.pdf。按下按钮我想让webbrowser显示一个空页面/什么都没有,我想将.pdf移动到另一个文件夹。首先,我尝试在移动之前导航到“”,但我的.pdf被另一个进程使用。谷歌告诉我,我可能需要清除浏览器的缓存才能移动它。我使用这里的代码http://www.gutgames.com/post/Clearing-the-Cache-of-a-WebBrowser-Control.aspx这样做了,我甚至尝试了在注释nr 2中找到的替代代码行,但是这些代码行都没有让我移动我的.pdf,它仍然被另一个进程使用。

能够/应该怎样做才能移动文件?我忘记了什么吗?

在第二个File.Move是我收到错误的地方:

webBrowser1.Navigate("");
WebBrowserHelper.ClearCache();
if (calConv != "")
{
    File.Move(forsDir + calConv + ".cal", forsDir + calConv.Replace("ToDo\\", "") + ".cg4");
    File.Move(forsDir + calConv + ".pdf", forsDir + calConv.Replace("ToDo\\", "") + ".pdf");
}

1 个答案:

答案 0 :(得分:0)

这是如何使用Adobe Reader而不使用webcontrol来显示PDF:

http://www.adobe.com/devnet/acrobat/downloads.html

下载Acrobat SDK

在你的项目中添加对SDK中两个dll的引用 - AxInterop.AcroPDFLib.dll和Interop.AcroPDFLib.dll

在表单的构造函数中添加Adobe预览器控件:

// Check if the user has Adobe Reader installed, if not you could show a link to Adobe Reader installer
if (Type.GetTypeFromProgID("AcroPDF.PDF") == null)
{
    pnlGetAdobe.Visible = pnlGetAdobe.Enabled = true;
}
else
{
    try
    {
        // Initialize the Adobe control
        axAcroPDF1 = new AxAcroPDF();
        axAcroPDF1.Dock = DockStyle.Fill;
        axAcroPDF1.Enabled = true;
        axAcroPDF1.Location = new Point(0, 25);
        axAcroPDF1.Name = "axAcroPDF1";
        axAcroPDF1.OcxState = (AxHost.State)new ComponentResourceManager(typeof(JasperPdfReport)).GetObject("axAcroPDF1.OcxState");
        axAcroPDF1.Size = new Size(634, 393);
        axAcroPDF1.TabIndex = 1;
        pnlCenter.Controls.Add(axAcroPDF1); // Add it to a container or instead directly to your form with this.Controls.Add(axAcroPDF1)
        axAcroPDF1.BringToFront();
    }
    catch (COMException cex)
    {
        axAcroPDF1.Dispose();
        axAcroPDF1 = null;
        MessageBox.Show(cex.ToString());
    }
    catch (Exception ex)
    {
        axAcroPDF1.Dispose();
        axAcroPDF1 = null;
        MessageBox.Show(ex.ToString());
    }
}

最后将PDF文件加载到控件中:

if (axAcroPDF1 != null && File.Exists(pdfFilename))
{
    axAcroPDF1.setShowToolbar(false);
    axAcroPDF1.setView("FitH");
    axAcroPDF1.setLayoutMode("SinglePage");
    // Load the PDF into the control
    axAcroPDF1.LoadFile(pdfFilename);
    axAcroPDF1.src = pdfFilename;

    // Show it
    axAcroPDF1.Show();
    axAcroPDF1.Refresh();
}
相关问题