【问题标题】:Error: The process cannot access the file '…' because it is being used by another process错误:该进程无法访问文件“...”,因为它正被另一个进程使用
【发布时间】:2014-04-08 16:37:52
【问题描述】:

我正在下载文件并将更改的文件发回给最终用户在 sharepoint 2010 中抛出 httphandler,但有时我收到错误“进程无法访问文件'...',因为它正在被另一个进程使用”这里是我的代码:

string myFile="MypdfFile.pdf";
string downloadFilePath = @"C:\Windows\Temp";
if (!Directory.Exists(downloadFilePath))
{
    Directory.CreateDirectory(downloadFilePath);
}

string downloadFilePathAndName = downloadFilePath + "\\" + myFile.Name;
newFile = downloadFilePathAndName;
content = myFile.OpenBinary();                                                     

using (PdfReader pdfReader = new PdfReader(content))
{                                                                                                  
    var fileStream = new FileStream(newFile, FileMode.Create, FileAccess.Write,FileShare.ReadWrite);
    var document = new Document(pdfReader.GetPageSizeWithRotation(1));
    var writer = PdfWriter.GetInstance(document, fileStream);
    document.Open();                                                        

    for (var i = 1; i <= pdfReader.NumberOfPages; i++)
    {
        document.NewPage();
        var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        var importedPage = writer.GetImportedPage(pdfReader, i);
        var contentByte = writer.DirectContent;
        contentByte.BeginText();
        contentByte.SetFontAndSize(baseFont, 12);
        string mytext = " ";                                                          

        contentByte.ShowTextAligned(2, mytext, 100, 200, 0);
        contentByte.EndText();
        contentByte.AddTemplate(importedPage, 0, 0);
    }

    document.Close();
    writer.Close();
    fileStream.Dispose();
    fileStream.Close();
    pdfReader.Dispose();
    pdfReader.Close();  

}

我已将 FileStream 保留在 using 语句中,但这也不能解决问题。

【问题讨论】:

  • 我不确定这是否是原因,但您正在以错误的顺序关闭和处理对象。首先,您需要关闭文件访问权限,然后处置对象。

标签: c# filestream


【解决方案1】:

FileStream 保留在另一个using 中本身是不够的——您还需要将it 包装在using() 语句中。你也不需要Dispose()你是using

试试这个(未经测试):

using (var pdfReader = new PdfReader(content))
using (var fileStream = new FileStream(newFile, FileMode.Create, FileAccess.Write,FileShare.ReadWrite))
using (var document = new Document(pdfReader.GetPageSizeWithRotation(1)))
using (var writer = PdfWriter.GetInstance(document, fileStream))
{
  document.Open();                                                        
  for (var i = 1; i <= pdfReader.NumberOfPages; i++)
  {
    document.NewPage();
    var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    var importedPage = writer.GetImportedPage(pdfReader, i);
    var contentByte = writer.DirectContent;
    contentByte.BeginText();
    contentByte.SetFontAndSize(baseFont, 12);
    string mytext = " ";                                                          

    contentByte.ShowTextAligned(2, mytext, 100, 200, 0);
    contentByte.EndText();
    contentByte.AddTemplate(importedPage, 0, 0);
  }
  // These lines may be unnecessary.
  writer.Close();
  document.Close();
  fileStream.Close();
  pdfReader.Close();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    相关资源
    最近更新 更多