user3070130
user3070130

Reputation: 9

File Not found in exe of wpf application

When publishing the WPF application and generate an exe, I am unable to get the files which are placed in the templates-folder. When I copy my folder and files to bin it works, or if use

string StartUpPath = AppDomain.CurrentDomain.BaseDirectory.ToString();
//  var gparent = Directory.GetParent(Directory.GetParent(Directory.GetParent(StartUpPath).ToString()).ToString()).ToString();

ReportDocument reportDocument = new ReportDocument();
StreamReader reader = new StreamReader(new FileStream(StartUpPath + @"\Templates\Invoice.xaml", FileMode.Open));

This code works fine for my local, but when I generate an exe, these files are not found.

Upvotes: 0

Views: 314

Answers (2)

Tobias Roland
Tobias Roland

Reputation: 1223

That's because your files aren't in the exe. I had the same problem recently.

It sounds like you need to handle your resources - there are several ways to go about this, and Microsoft has a full explanation here about resources, including the difference between using Linked and Embedded Resources, and links through to other great guides.

I'm assuming you're using Visual Studio, in which case this should work;

  • right click the file you can't access
  • select properties (or press ALT + ENTER)
  • set Build Action to be Resource
  • set Copy to Output Directory to be Copy if newer
  • save

... and you should be good to go.

Upvotes: 1

Ekk
Ekk

Reputation: 5715

I think it throws an exception because the file doesn't exist on that machine. You can set Build Action to Content and Copy to Output Directory to Copy Always or Copy If Newer on property window.

These settings will make sure that you have the file to your output.

enter image description here

Upvotes: 0

Related Questions