Podgląd dokumentów word (docx) w windows form

0

Witam,
skorzystałem z kodu który znajduje się na stronie http://codinglight.blogspot.com/2008/10/simple-docbrowser-control.html
pozwala mi on wyświetlać zawartość dokumentu w okienku programu. Niestety gdy próbuje użyć ponownie

 docBrowser1.LoadDocument("Ścieżka do pliku") 

wyświetla się błąd IOException was unhandled by user code do linijki kodu

  File.Delete(tempFileName);

. Proszę o pomoc w zrozumieniu błędu.

Kod programu:

using System;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace WordControls
{
    public partial class DocBrowser : UserControl
    {
        private System.Windows.Forms.WebBrowser webBrowser1;

        delegate void ConvertDocumentDelegate(string fileName);

        public DocBrowser()
        {
            InitializeComponent();

            // Create the webBrowser control on the UserControl. 
            // This code was moved from the designer for cut and paste
            // ease. 
            webBrowser1 = new System.Windows.Forms.WebBrowser();

            webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
            webBrowser1.Location = new System.Drawing.Point(0, 0);
            webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            webBrowser1.Name = "webBrowser1";
            webBrowser1.Size = new System.Drawing.Size(532, 514);
            webBrowser1.TabIndex = 0;

            Controls.Add(webBrowser1);

            // set up an event handler to delete our temp file when we're done with it. 
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
        }

        string tempFileName = null;

        public void LoadDocument(string fileName)
        {
            // Call ConvertDocument asynchronously. 
            ConvertDocumentDelegate del = new ConvertDocumentDelegate(ConvertDocument);

            // Call DocumentConversionComplete when the method has completed. 
            del.BeginInvoke(fileName, DocumentConversionComplete, null);
        }

        void ConvertDocument(string fileName)
        {
            object m = System.Reflection.Missing.Value;
            object oldFileName = (object)fileName;
            object readOnly = (object)false;
            ApplicationClass ac = null;

            try
            {
                // First, create a new Microsoft.Office.Interop.Word.ApplicationClass.
                ac = new ApplicationClass();

                // Now we open the document.
                Document doc = ac.Documents.Open(ref oldFileName, ref m, ref readOnly,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                     ref m, ref m, ref m, ref m, ref m, ref m);

                // Create a temp file to save the HTML file to. 
                tempFileName = GetTempFile("html");

                // Cast these items to object.  The methods we're calling 
                // only take object types in their method parameters. 
                object newFileName = (object)tempFileName;

                // We will be saving this file as HTML format. 
                object fileType = (object)WdSaveFormat.wdFormatHTML;

                // Save the file. 
                doc.SaveAs(ref newFileName, ref fileType,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m);

            }
            finally
            {
                // Make sure we close the application class. 
                if (ac != null)
                    ac.Quit(ref readOnly, ref m, ref m);
            }
        }

        void DocumentConversionComplete(IAsyncResult result)
        {
            // navigate to our temp file. 
            webBrowser1.Navigate(tempFileName);
        }

        void webBrowser1_DocumentCompleted(object sender,
            WebBrowserDocumentCompletedEventArgs e)
        {
            if (tempFileName != string.Empty)
            {
                // delete the temp file we created. 
                File.Delete(tempFileName);

                // set the tempFileName to an empty string. 
                tempFileName = string.Empty;
            }
        }

        string GetTempFile(string extension)
        {
            // Uses the Combine, GetTempPath, ChangeExtension, 
            // and GetRandomFile methods of Path to 
            // create a temp file of the extension we're looking for. 
            return Path.Combine(Path.GetTempPath(),
                Path.ChangeExtension(Path.GetRandomFileName(), extension));
        }
    }
} 
0

Problem może leżeć w tym, że obiekt jest dalej wykorzystywany przez inny wątek. Spróbuj uruchomić delegat w głównym wątku:

    del.Invoke(fileName, DocumentConversionComplete, null); 
0
            if (tempFileName != null)
            {
                // delete the temp file we created. 
                File.Delete(tempFileName);
 
                // set the tempFileName to null
                tempFileName = null;
            }
0

Niestety nie daje rady. Nie mogę dodac

del.Invoke(fileName, DocumentConversionComplete, null);  

a if (tempFileName != null)
{
// delete the temp file we created.
File.Delete(tempFileName);

            // set the tempFileName to null
            tempFileName = null;
        } 
 po wprowadzeniu tego nic się nie zmienia dalej wyrzuca bląd.
0

z jakiegoś powodu to File.Delete rzuca wyjątek.
brak pliku? brak uprawnień? parametr jest nullem?

zobacz co się dzieje, opakuj w try, jeśli potrzeba... programuj z głową, a nie tylko w oparciu o snippety z sieci.

0

Błąd z try

System.IO.IOException: Proces nie może uzyskać dostępu do pliku „H:\aamql1fn.html”, ponieważ jest on używany przez inny proces.
   w System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   w System.IO.File.InternalDelete(String path, Boolean checkHost)
   w eWniosek.DocBrowser.webBrowser1_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e) w  

Niestety nie mam pojęcia jak się do tego zabrać.

0

Sprawdź jakimś resource monitorem albo unlockerem, jaki proces trzyma ten plik.

1 użytkowników online, w tym zalogowanych: 0, gości: 1