How to create an installer for a wpf project to include a local file

117 views Asked by At

I have written a wpf application that does some work on a dictionary based on user interaction with the ui. I save the changes made to the dictionary to a txt file that I have manually created inside my solution.

I now want to deploy my application and make it installable and usable on another machine. For this purpose, I have created a Setup project with the Visual Studio Setup template. After the installation on my machine, the program crashed. I suspect this was because of the file not being accessible.

My question is how can I get my project to ship with an empty text file which will be present in the client's computer and hold the changes made to the dictionary in it? I am aiming to use the txt file that I will ship with my project withing the setup.

public Dictionary<string, string> LoadDictionary()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();

            if (File.Exists(FilePath))
            {
                using (StreamReader reader = new StreamReader(FilePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] parts = line.Split('=');
                        if (parts.Length == 2)
                        {
                            string key = parts[0];
                            string value = parts[1];
                            dictionary[key] = value;
                        }
                    }
                }
            }

            return dictionary;
        }

This is the load method I use to load my dictionary. The filepath is no issue when the text file is present in the solution directory, but how can I ensure that the program always gets the path that belongs to the txt file that comes within the installer?

string installationFolder = AppDomain.CurrentDomain.BaseDirectory;
string filePath = System.IO.Path.Combine(installationFolder, "Logs.txt");
InstallationFolder = installationFolder;
FilePath = filePath;

This is how the paths get initialised within the MainWindow() c onstructor of my app. FilePath and InstallationFolder are string properties, defined outside of the MainWindow() constructor.

1

There are 1 answers

0
Demon On

I am not sure what you are trying to achieve or what issue you are facing, so I will provide just a couple of recommendations based on the assumptions.

  1. Try to use AppData location for files that are used withing you application (unless you are targeting old windows versions). To do that instead of "AppDomain.CurrentDomain.BaseDirectory" use "Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData"
  2. Unless you must ship your app with predefined dataset, I suggest generating the file if they are missing (*.txt in your case). Remember that file can get corrupted, deleted, renamed, etc. 3.I never had to use VS Installer template, but I suggested to move to third party installer, since they provide more flexibility, and it is easier to implement them in CI/CD