【发布时间】:2016-05-26 06:05:34
【问题描述】:
PD4ML pd4ml = new PD4ML();
pd4ml.enableTableBreaks(true);
pd4ml.PageInsets = new System.Drawing.Rectangle(5, 5, 5, 5);
pd4ml.PageSize = PD4Constants.getSizeByName("LETTER");
Byte[] byteArray = Encoding.ASCII.GetBytes(content);
MemoryStream stream = new MemoryStream(byteArray);
FinalPath = FinalPath + @"\" + VersionID;
if (!Directory.Exists(FinalPath))
Directory.CreateDirectory(FinalPath);
string FileName = FinalPath +FileName+ ".pdf";
pd4ml.render(stream,new System.IO.FileStream(FileName, System.IO.FileMode.CreateNew));
stream.Flush();
stream.Close();
stream.Dispose();
//In another method I'm opening this file
File stream fs = File.Open(path, FileMode.Open, FileAccess.Read);`
我正在使用 pd4ml.render() 方法生成 PDF。当我使用渲染方法创建这个文件时,它会在系统内部的某个地方打开。这就是为什么当我尝试使用 Filestream fs=new Filestream(path,FileMode.Open,FileAccess.Read) 手动打开它时
它抛出异常并且文件正在被另一个进程使用。请指导我该怎么做。
我已经在我的代码中使用了 FileShare.ReadWrite 属性和 File.OpenRead(path),但它对我不起作用。
【问题讨论】:
-
尝试使用 (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
-
为什么不先关闭呢?
-
但要关闭它,我必须先打开它,当我尝试打开它时,它给了我异常。而且我使用过 FileShare.ReadWrite 但它对我不起作用。
-
你在创建一个新文件吗??
-
是的,我正在使用 pd4ml.render(stream, new System.IO.FileStream(FileName, System.IO.FileMode.CreateNew)) 创建一个新文件。在这个创建文件在系统内部某个我无法检测到的地方打开之后。