menu

秋梦无痕

一场秋雨无梦痕,春夜清风冻煞人。冬来冷水寒似铁,夏至京北蟑满城。

Avatar

C#中的静态连接

昨天有人问我一个问题,说是他做的水晶报表转成pdf的工具,不能在别的机器上使用。初步估计是静态连接的问题。

于是从MSDN中找出以下代码:

//file: Rpt2Pdf.cs
using System;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace ifyr.Util {
class Rpt2Pdf {
[STAThread]
static void Main(string[] args) {
//检查参数
string rptFile = null;
string pdfFile = null;
if(args.Length == 1 && args[0].EndsWith(".rpt")) {
rptFile = args[0];
pdfFile = rptFile.Substring(0,rptFile.Length - 3) + "pdf";
} else if (args.Length == 2 && args[0].EndsWith(".rpt") && args[1].EndsWith(".pdf")) {
rptFile = args[0];
pdfFile = args[1];
} else {
Console.WriteLine("Usage: Rpt2Pdf [path]reportfile [[path]pdffile]");
return;
}

ReportDocument Report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
PdfRtfWordFormatOptions pdfFormatOpts = new PdfRtfWordFormatOptions();
DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
//如果文件不存在,在这儿会报错的,只没有做Exception的处理。
Report.Load(rptFile);
Report.Refresh();

// 设置格式选项。
ExportOptions exportOpts = new ExportOptions();
exportOpts = Report.ExportOptions;
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
exportOpts.FormatOptions = pdfFormatOpts;

// 设置磁盘文件选项并导出。
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
diskOpts.DiskFileName = pdfFile;
exportOpts.DestinationOptions = diskOpts;

Report.Export();
}
}
}

项目中添加CrystalDecisions.CrystalReports.Engine和CrystalDecisions.Shared的引用,编译,运行,正常。

把exe拷贝到另一个目录……就找不到CrystalDecisions了……拷贝dll过去也没有用……

把CrystalDecisions.CrystalReports.Engine.dll和CrystalDecisions.Shared.dll两个文件拷贝到project文件夹(cs文件所在目录),然后编译:
csc /lib:. /r:CrystalDecisions.CrystalReports.Engine.dll,CrystalDecisions.Shared.dll Rpt2Pdf.cs
再把生成的Rpt2Pdf.exe拷贝到其他目录运行,还是找不到CrystalDecisions……再把dll拷贝过去,就可以运行了。

在项目中把引用的属性"拷贝到本地"设成True,这时,project文件夹下的bin/debug文件夹中出现一个zh-CHS文件夹,重新生成项目,该文件夹下面就有了两个文件:CrystalDecisions.Shared.resources.dll和CrystalDecisions.CrystalReports.Engine.resources.dll,把这两个文件拷贝到project文件夹,编译:
csc /lib:. /r:CrystalDecisions.CrystalReports.Engine.dll,CrystalDecisions.Shared.dll
/res:CrystalDecisions.CrystalReports.Engine.resources.dll,CrystalDecisions.Shared.resources.dll Rpt2Pdf.cs
生成的exe文件大了7k……把exe拷贝到其他目录执行,不再报错,问题解决。

评论已关闭