Crystal Report - 文件存在错误

时间:2018-06-18 09:14:09

标签: asp.net windows crystal-reports

我正在使用crystal report(运行时版本13.0.15)从asp.net Web应用程序生成PDF报告。随机使用报告时,我得到一个显示停止文件锁定错误 - " System.IO.IOException - 该文件存在。 ",之后我的报告都不起作用。但该应用程序运行正常。我做了IIS重置,但问题存在。

然后我从" C:\ Windows \ Temp"中删除了临时文件。文件夹,然后所有报告都正常工作。

这是源代码,我用来生成报告。我没有使用CR查看器。所有报告都导出为PDF。

var __reportData = GetData(filter);
ReportDocument rd = new ReportDocument();
try
{
rd.Load(rptFilePath);
rd.SetDataSource((System.Data.DataTable)__reportData);
System.IO.Stream oStream = null;
byte[] byteArray = null;
oStream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
byteArray = new byte[oStream.Length];
oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
rd.Close();
rd.Dispose();
rd = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
catch (Exception ex)
{
    if (rd != null)
    {
        rd.Close();
        rd.Dispose();
        rd = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }
    CrystalReportExceptionHandler.ShowError(ex, ex.Message);
}
finally
{
    if (rd != null)
    {
        rd.Close();
        rd.Dispose();
        rd = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }
}

4 个答案:

答案 0 :(得分:0)

通过更改以下代码行来查看问题是否消失:

rd.Load(rptFilePath);

rd.Load(rptFilePath, OpenReportMethod.OpenReportByTempCopy);

第二版确保使用报告的临时副本。

答案 1 :(得分:0)

我建议使用像这样的代码块用C#重写代码:

var __reportData = GetData(filter);
using(ReportDocument rd = new ReportDocument()) {

 byte[] byteArray = null;
 rd.Load(rptFilePath);
 rd.SetDataSource((System.Data.DataTable)__reportData);

  using(System.IO.Stream oStream oStream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)) {
    byteArray = new byte[oStream.Length];
    oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
  }
}

使用block,无需在成功或异常时手动调用.Close或.Dispose方法。另外,请注意使用块将System.IO.Stream包装在内部,以确保它在原始源代码中丢失了。

答案 2 :(得分:0)

.NET中记录了此异常,当temp目录达到65535个文件的限制时会引发此异常。

答案 3 :(得分:-1)

空 c:/windows/temp 对我有用