【发布时间】: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