Reputation: 1918
I have tu publish a windows form application in c#. The problem is that I have a folder named "dati" in project folder
In code, when I try to read from folder I use this instruction
string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName;
and when I try to read an xls inside this folder I have this instruction
string path = projectDirectory + "\\dati\\dati.xlsx";
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=NO;\"";
In debug all works perfectly, also if I execute the .exe inside bin/Debug or bin/Release folder. When I publish the application, and put it in another folder like C:\ProjectPublished I get this error because it is trying to read the dati folder from another path.
What I have to do? Thanks
Upvotes: 0
Views: 2084
Reputation: 4196
Use this:
string projectDirectory = AppDomain.CurrentDomain.BaseDirectory ;
This will give you the directory where your executable is residing.
Upvotes: 0
Reputation: 406
The simplest way to work with data files is to include them in the project as a content item. Use the right click your *.xlsx file in the Solution Explorer and set Build Action to Content and Copy to the output directory to Always. It will make the build engine include the data file to the /bin directory. Then you may need to set up your deploy engine to include this file in the deployable package. For instance, if you are using ClickOnce, you may need to include this file in Publish tab of your project
Upvotes: 2