2012-06-26 22 views
2

我寫了下面的代碼,設置在液晶的相對路徑報告

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

報告工作正常,上面的代碼,但我想給一個相對路徑,所以我可以在多臺計算機上安裝它在不改變路徑。 我也曾嘗試以下路徑

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

把他們當作,

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

但文件不能被accessed.Please幫助。

+0

什麼'Environment.CurrentDirectory'的根目錄中?它通常是相對文件的基礎。 –

+0

你是什麼意思'文件無法訪問'?嘗試使用Load()時是否收到異常? – ScottieMc

+0

爲什麼不將報告添加到您的項目中,以便它永遠是您的解決方案的一個組成部分,並且更加「異常安全」?這將使得需要從外部路徑加載它已過時... – Urik

回答

3

尚未在Windows應用程序中測試過,但這是我在MVC3中使用的。我認爲,相對路徑必須是你的應用程序,雖然

using CrystalDecisions.CrystalReports.Engine; 
    using CrystalDecisions.Shared; 

    using (var report = new ReportClass { FileName = Server.MapPath("/AppName/Reports/MyReport.rpt") }) 
    { 
     report.Load(); 
    ... 
0
private void Crystal_rpt() 
{ 
    ReportDocument dir = new ReportDocument(); 
    dir = functions.Show_Crystal_rpt(); 
    dir.Load(Application.StartupPath + "\\rptObt_mark.rpt"); 
    crpMark_sheet.ReportSource = dir; 
}