Search code examples
c#crystal-reports

Setting a relative path in crystal reports


I have written the following code,

CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
report.Load(@"C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt");

Report works fine for the above code But I want to give a relative path so I can install it in several machines without changing the path. I have also tried the following paths

string loc = AppDomain.CurrentDomain.BaseDirectory;
string loc2 = Application.StartupPath;
string loc3 = Application.UserAppDataPath;
string loc4 = Application.CommonAppDataPath;                

Used them as,

report.Load(loc2 + "\\ReportSalesInvoice.rpt");

But the file cannot be accessed.Please Help.


Solution

  • haven't tested in windows app, but this is what I use in MVC3. I believe the relative path has to be within the root of your application though

        using CrystalDecisions.CrystalReports.Engine;
        using CrystalDecisions.Shared;
    
        using (var report = new ReportClass { FileName = Server.MapPath("/AppName/Reports/MyReport.rpt") })
        {
            report.Load();
        ...