【问题标题】:error The process cannot access the file because it is being used by another process错误进程无法访问该文件,因为它正被另一个进程使用
【发布时间】:2013-06-18 06:14:05
【问题描述】:

在此我正在尝试将 word 转换为 pdf 文件。但我收到一个错误 “该进程无法访问该文件,因为它正被另一个进程使用”。

    public Microsoft.Office.Interop.Word.Document wordDocuments { get; set; }

    Microsoft.Office.Interop.Word.Application apword = new Microsoft.Office.Interop.Word.Application();
    try
    {
        if (uploadFInput.HasFile)
        {
            targetPathip = Server.MapPath(Path.GetFileName(uploadFInput.PostedFile.FileName));

            if (File.Exists(targetPathip))
            {
                File.Delete(targetPathip);
            }

            string Extentsion_path = Path.GetExtension(targetPathip);
            string Filename_path = Path.GetFileName(targetPathip);
            if (Extentsion_path == ".doc" || Extentsion_path == ".docx")
            {
                uploadFInput.SaveAs(targetPathip);
                LblFleip.Text = targetPathip;

                //wordDocuments = apword.Documents.Open(Filename_path);
                wordDocuments = apword.Documents.Open(Filename_path);
                // wordDocuments = apword.Documents.Open(targetPathip);

                wordDocuments.ExportAsFixedFormat(Filename_path, WdExportFormat.wdExportFormatPDF);
                apword.Documents.Close(Filename_path);
            }
            string filename = Path.GetFileName(targetPathip);
            uploadFInput.SaveAs(targetPathip);
            LblFleip.Text = targetPathip;
        }
    }
    catch (Exception ex)
    {
        apword = null;
        return;
    }

转换时我的代码中是否缺少任何内容?谁能告诉我如何将word转换为pdf。

【问题讨论】:

  • wordDocuments = apword.Documents.Open(Filename_path) .. wordDocuments.ExportAsFixedFormat(Filename_path, X) - 相同的文件名看起来非常可疑。
  • @user2246674 它曾在 Windows 窗体中工作过相同的代码 ....
  • 运行这段代码时桌面上没有打开实际的word文档是吗?
  • 你确定那是它崩溃的实际线路,你有没有通过? (你应该在 apwords 声明周围有一个 using 块
  • File.Delete(targetPathip);问题出在这一行。如果文件已经存在,那么它应该删除 .但是在那个地方我收到一个错误进程无法访问该文件,因为它正在被另一个进程使用

标签: c# asp.net visual-studio-2010 pdf ms-word


【解决方案1】:

您需要做的是确定保持文件打开的进程。

要找到这一点,您需要从 SysInternals(现在是 Microsoft 的一部分)下载 Process Explorer。

这将允许您搜索所有进程以找出哪些进程对您的文件具有打开的句柄。

当然,文件冲突可能与不太明显的文件有关,例如配置或锁定文件。在这种情况下,进程监视器(也来自 SysInternals)应该允许您查看失败的原因。

两者都是出色的工具,一旦您使用它们,它们就会成为您军械库的一部分。

【讨论】:

    【解决方案2】:

    如果您是第一次运行代码 - 可能会没事的。但很明显,文件没有正确处理。 Standart .net GC 不能正确处理Interop 对象。所以,一些提示:
    1) 请勿使用Interop.Word.DocumentInterop.Word.Application 制作公共属性。在您的方法中使用它们并尽快处理。
    2) 尽快关闭Application 的第二个原因是RPC 服务器超时。如果您将Application 保持打开一段时间,它将自动与 RPC 服务器断开连接,您将无法使用它。

    考虑到这一点,您必须在finally 语句中调用apword.Quit();,而不是在catchapword = null 中单独调用是不够的。
    如果应用程序因错误而关闭 - 肯定会终止 word 进程。
    并且不要忘记在离开方法之前调用wordDocuments.Close(ref SaveChanges)

    【讨论】:

      【解决方案3】:

      该变量包含带扩展名的文件名,因此您尝试在打开的文档上保存文档。添加扩展:

       wordDocuments.ExportAsFixedFormat(Filename_path + ".pdf",
           WdExportFormat.wdExportFormatPDF);
      

      我猜这行 File.Delete(targetPathip); 不是问题,可能在第二次运行时,因为应用程序从第一次运行时保持文件打开(请参阅任务管理器)。

      【讨论】:

      • 我想知道这是否是第二次运行,如果是这种情况,可以通过正确处理 apword 来解决此问题
      【解决方案4】:

      曾经遇到过这样的问题,请在打开文件之前尝试关闭文件(听起来很愚蠢,但对我有用......)。

      apword.Documents.Close(Filename_path);
      wordDocuments = apword.Documents.Open(Filename_path);
      

      【讨论】:

      • 异常:@3wic 此方法或属性不可用,因为文档窗口未处于活动状态。
      【解决方案5】:

      你只需要添加

      using System.Threading;
      

      在cs页面的顶部.. 并添加

      Thread.SpinWait(6000);
      

      在显示错误的文本顶部。

      试试这个,如果你需要任何帮助告诉我。

      【讨论】:

      • 我建议不要这样做...似乎它只会延迟问题
      • @mitesh 这只会延迟它。如果 pdf 的文件或单词非常大。这很难。
      • 我同意@sayse 这会延迟
      • 我试过了,它对我有用。但我已经让它用于制作我的图像的副本,而不是让它删除后记
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      相关资源
      最近更新 更多