visual-studiowinformsexevisual-studio-publish

c# publish exe of windows form application with folder data


I have tu publish a windows form application in c#. The problem is that I have a folder named "dati" in project folder enter image description here

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.

enter image description here

What I have to do? Thanks


Solution

  • 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