Include data files when deploying

brainleak

Well-known member
Joined
Dec 20, 2016
Messages
54
Location
Barcelona area
Programming Experience
3-5
In the directory

...\AppName\AppName\bin\Debug\

I have a settings file, AppName.Settings and a text file Data.txt which is also read at run time.

How must I handle them in the deploy project?
 
Firstly, I'm wondering why you have a Settings file in the output folder. If you have added a Settings file to your project then it should be being compiled into the executable and you just access the settings in code via the class name, which matches name you gave the Settings file. The default values for the settings are stored in the standard config file and any values you save for User-scoped settings will be stored in a user config file. The Settings file doesn't get separately deployed with the app.

As for the text file, it should be included in a publish by default but, if it's not, do the following:

1. Select the file in the Solution Explorer.
2. Open the Properties window and make sure that the Build Action is set to Content and 'Copy to Output Directory' is set to 'Copy if newer'.
3. Open the Publish page of the project properties, click the Application Files button, check the 'Show all files' box and set the Publish Status of your file to Include.
 
In the directory

...\AppName\AppName\bin\Debug\

I have a settings file, AppName.Settings and a text file Data.txt which is also read at run time.

How must I handle them in the deploy project?
JMC has already addressed the settings file, either include it in your executable or better yet have the program look for the settings file at first run and if it's not there, create it with all default values; I like to make my own settings file and I usually store it in the user's appdata folder.

As for the data file what JMC describes will work, but you could run into issues with that if the user where to use your installer and install it somewhere like the Program Files or Program File (x86) folder where your application wouldn't be allowed to write to it, you can read it from there, but not write. However there are two options to overcome this: you could, like the settings file, put it in the user's AppData folder but this will only allow the 1 user to use it as other "users" (ie Windows accounts) wont be able to see the file in which case if you need to allow multiple windows users to use the file I would recommend the ProgramData folder, which is usually a hidden file right on the root of the drive Windows is installed on, typically C:\ProgramData

Just a couple of things to keep in mind.
 
As for the data file what JMC describes will work, but you could run into issues with that if the user where to use your installer and install it somewhere like the Program Files or Program File (x86) folder

I was assuming ClickOnce and that that wouldn't happen but, upon rereading the OP I see that there's nothing there to imply that. In that case, the OP really needs to tell us what tool they're using to create their deployment package because the specifics may vary.
 
Thanks to the both of you for your answers.

About the settings file:


I used an example from a web site which read some variable values (in this case a default directory path) from a file:


VB.NET:
Private ConfigPathname As String = Application.StartupPath & "\" & Application.ProductName & ".Settings"


     Private Sub LoadSettings()


        If New FileInfo(ConfigPathname).Exists Then


            Dim xDS As New Settings
            Dim xRow As Settings.MainRow


            xDS.ReadXml(ConfigPathname, System.Data.XmlReadMode.IgnoreSchema)


            If xDS.Main.Rows.Count > 0 Then


                xRow = xDS.Main.Rows.Item(0)


                If Not xRow.IsDefDirNull() Then
                    defaultDir = xRow.DefDir
                End If


            End If


        End If


    End Sub

Then at program close it stored the values to the file using this code:


VB.NET:
    Public Sub SaveSettings()


        Dim xDS As New Settings
        Dim xRow As Settings.MainRow


        xRow = xDS.Main.NewMainRow
        xRow.DefDir = defaultDir
        xDS.Main.AddMainRow(xRow)
        xDS.WriteXml(ConfigPathname, System.Data.XmlWriteMode.IgnoreSchema)


    End Sub

The idea was that, this application being for my own use, I could edit the file rather than modifying the code + recompile + reinstall it. And indeed, if the file is not there when the program looks for it, I supply default values as JuggaloBrotha suggests. For this case it's enough to just manually copy the file to the application directory after installation has been completed but, what if I meant to distribute it? I don't quite understand how the settings should compile with the rest of the code if they are in a file.


As for the data text file, thanks for the detailed explanation, I'll think about how best to do it, whether place it in the hidden ProgramData folder or use the Settings file.
At any rate, when I select it in the solution explorer, it does not show any Build Action at all. The only properties I see are Custom Tool, Custom Tool Namespace, File Name and Full Path. I'm using Visual Studio 2015 Community.


By the way, I have no idea what that ClickOnce is!
 
I already solved the settings problem using the My.Settings approach.

As for the data text file, when I selected it in the solution explorer, it didn't show any Build Action at all... because I had yet to right click on it and select "Include in project". After I've done this I have gone to the properties window, set the build action to Content and 'Copy to output directory' to 'Copy if newer'. Yet the file IS NOT copied when I run the setup project. Is there any other action I should take?
 
So, if you're using VS 2015, you must have added the extension to support Installer Projects, correct? I've not created an installer using that but it is supposed to pretty much mimic the old, inbuilt Setup projects so, if that's the case, you have to add the appropriate project output to your installer. The primary output, which includes the EXE and the config file, is added by default. You need to also add the Content output to include items marked as Content.

I just created a Setup Wizard project and step 3 of 5 asks which outputs to include, two of which are Primary and Content Files, which are the ones you want. That said, if you file isn't marked as Content, step 4 of 5 also asks which additional files you want to include. If you've already been through the wizard, you can right-click on the project and select Add > Project Output in order to include the Content Files in your setup.
 
So, if you're using VS 2015, you must have added the extension to support Installer Projects, correct? I've not created an installer using that but it is supposed to pretty much mimic the old, inbuilt Setup projects so, if that's the case, you have to add the appropriate project output to your installer. The primary output, which includes the EXE and the config file, is added by default. You need to also add the Content output to include items marked as Content.

I just created a Setup Wizard project and step 3 of 5 asks which outputs to include, two of which are Primary and Content Files, which are the ones you want. That said, if you file isn't marked as Content, step 4 of 5 also asks which additional files you want to include. If you've already been through the wizard, you can right-click on the project and select Add > Project Output in order to include the Content Files in your setup.

Thanks a lot for the detailed explanation. Now it seemed to work as the data file was indeed included in the setup files and copied at installation.

However there's yet another issue: the file is not found when the app tries to read it (whereas there was no problem when debugging the app in the IDE).

In the project files, the data filepath is:
\AppName\AppName\bin\Debug\data.txt

After installation it turns out to be:
\Program Files\AppName\bin\Debug\data.txt

And the file is referred to in the code in this fashion:
Dim DataFile As String = Application.StartupPath & "" & "data.txt"

I wonder what may have gone awry this time.

(EDIT: for some reason a backslash in the previous path has vanished after sending the post)
 
...
In the project files, the data filepath is:
\AppName\AppName\bin\Debug\data.txt

After installation it turns out to be:
\Program Files\AppName\bin\Debug\data.txt

And the file is referred to in the code in this fashion:
Dim DataFile As String = Application.StartupPath & "" & "data.txt"
...
(EDIT: for some reason a backslash in the previous path has vanished after sending the post)
I inserted a messagebox.show statement:
VB.NET:
MessageBox.Show(DataFile)

and deployed again the app. Well, the messagebox displays
VB.NET:
DataFile = "C:\Program Files\AppName\data.txt"

but file is placed during the installation process in
VB.NET:
C:\Program Files\AppName\bin\Debug\data.txt
 
How can I have the setup project copy the file to this directory? If it's originally there (at design/debug time) then I can't select it from the solution explorer. Sorry I may seem so thickheaded but I'm a newbie so could you provide more details?
To do that, I would not have the data file itself as part of the Visual Studio project/solution, I would instead keep it on the side and have your installer reference it in the Program Data folder. Here's a (large) screenshot showing how to set that up:
  1. Create the custom folder, name it "ProgramData"
  2. Set the custom folder path to: %SYSTEMDRIVE%\ProgramData
  3. Add the file reference to the folder

VS Custom Folder.png
 
Back
Top