问题
I am automatically creating a PDF from some of our reports in a month-end process. I am running into a problem where ReportViewer.LocalReport
can't find my report. Within the project, the report files are in "(project root folder)/Reports/report.rdlc".
How do I set ReportViewer.LocalReport.ReportPath
so I can reference my report file?
I would rather not set the full path because I don't know where it would be installed when installed on the client machines.
回答1:
Use the Application.StartupPath property, it always points to directory where your EXE is located:
using System.IO;
...
string exeFolder = Application.StartupPath;
string reportPath = Path.Combine(exeFolder, @"Reports\report.rdlc");
You'll want to make sure the report gets copied to your bin\Debug\Reports folder as well so it will work in the IDE. Use xcopy /s /d in a post-build event to get the file(s) copied.
回答2:
The path is always relative to your "current directory" which in most cases is exe file- change rdlc files so that they are copied to the destination folder instead of embedded in resources(usually that's how this is done). The path in your project doesn't matter.
回答3:
You can store the reports in a "resources" file, and by using an Assembly reader of the given .dll / .exe where the "resource" file is embedded, can read as a streaming reference. Then set your report to the embeded stream to get the report definition.
Special note. If your report has nested sub-reports, the report will fail unless you also set in your LocalReport subreport definition another such reference to your subreport .rdlc file.
回答4:
There's a bug from what I understand that is coming from Windows XP. The issue does not exhibits itself on Windows 7.
All the above answers stand, but I have a correction.
This issue exhibits itself once you use a SaveFileDialog
which looks like changes the path of the current application. This happens when you use the current path, relative to current wokring directory which is the default for loading RDLC, ie reportpath = "nameOfReport.rldc"
. If you want to refresh the report after having used the SaveFileDialog the reportviewer cannot find the path to the report file.
The solution comes from Hans Passant on the post above but I would change from this
string exeFolder = Path.GetDirectoryName(Application.StartupPath);
to this
string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
Thanks Hans
回答5:
Simple Solution is..
Default Path of project is projectname/bin/debug
in Report Viewer code
Reportpath = "../../folder1/report1.rdlc"
The default path is projectname/bin/debug but if you create rdlc at root path for that you have to go at root but .net not provide such function so simply add ../ to change current directory to go perent directory.
i place ../../ two time bcz my rdlc at the root path so i have to change debug then bin and finally i reach the root path. if you have separate folder for rdlc then make sure that you that to write the foldername and place / our their.
Hope you like it..
来源:https://stackoverflow.com/questions/2702088/getting-the-relative-path-to-the-rdlc-report-in-my-winform-app