简体   繁体   中英

Error reading resource file 'C:\..Report.rpt' — 'The system cannot find the file specified. '

Continuing working from previous thread here .

What I have done is copied and paste the .rpt files from where the .cs files are located and moved in the \\bin\\Debug folder. It works but when I deleted the .rpt files where the .cs files are I get the error...

Error reading resource file 'C:..Report.rpt' -- 'The system cannot find the file specified. '

I created a dynamic path in the btn method...

private void btn_Click_1(object sender, EventArgs e)
    {
       ReportDocument cryRpt = new ReportDocument();
       cryRpt.Load(Application.StartupPath + "\\Report.rpt");
       crystalReportViewer1.ReportSource = cryRpt;
       crystalReportViewer1.Refresh();
    }

Couple of questions are going through my head because I will be using Setup Project and I little confuse because of my lack of knowledge and experience.

1) Should I leave the .rpt files where .cs files and copy and paste the .rpt files in \\bin\\Debug folder?

2) Do I need to do something else? Which leads to another question...what do I do?

3) When I look into the properties of .rpt file I can see full path directory.

So I am looking for guide.

Thanks in advance if anyone can help me here.

You can include your rpt files in your project and mark them as:

  • Build Action: None (as they are not built)
  • Copy to Output: Copy always or Copy if newer - this way they will be output to the same folder as your binaries (in your case \\bin\\Debug).

Okay .net is defined to use what's called Xcopy deployment. ie all the files required by the application end up in the output folder. After a build you can copy that folder "anywhere" and your code will work.

It does that by copying your "extra" files from where ever they are on your machine, to the output folder. As @Syzmon directed, set the property to copy if newer or copy always , and that's it.

Obviously in order to copy a file to somewhere the original must exist. When you deleted them from your code folder you left VS in the same position as you would if you deleted your cs files and then wondered why you weren't getting an exe...

So basically you are doing it right, just don't delete the originals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM