-1

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.

Jeff
  • 33
  • 5
  • I am not 100% clear on whether you are planning on distributing a seeded textfile or if installations shall start out with an empty file? If you want to deploy a seeded (= non-empty) file, you explictly need to include it in the setup, so it will be copied. Haven't used the Setup tmeplate, before, so I am not of any help in the details, I am afraid. – Fildor Jul 11 '23 at 13:44
  • I have tried including an seeded file within the setup, but the program just crashed when it needed to write to that file. Thank you though for your input. – Jeff Jul 11 '23 at 13:46
  • 2
    Have you tried to make sure its because of the missing file with handling the exception? – Saiko Jul 11 '23 at 13:48
  • ^^ Me too, I think it's worth it to investigate into that exception. _Maybe_ you need to configure access rights for the user that's executing your app? – Fildor Jul 11 '23 at 13:49
  • The file should not reside in the installation folder, but in a folder where your application has write permission, e.g. in a subfolder of [Environment.SpecialFolder.ApplicationData](https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-7.0). You do not need to install it there. Just create the file (and the subfolder) the first time it should be written to. – Clemens Jul 11 '23 at 13:58
  • @Clemens OP seems to be wanting the file to be seeded with data. Of course, that could also be done programmatically. But I don't think that part is directly related to the problem being observed. – Fildor Jul 11 '23 at 14:03
  • @Fildor "*how can I get my project to ship with an empty text file*" - so, no. The "*problem being observed*" is the missing write permission on any file in the installation folder. – Clemens Jul 11 '23 at 14:04
  • @Clemens Yeah, OP is sending mixed signals, there ... but we agree on the actual problem. – Fildor Jul 11 '23 at 14:05
  • The issue was indeed related to permission problem. Running the application as administrator helped solve the issue. Thank you all for you time and input. Have a nice day. – Jeff Jul 11 '23 at 14:52
  • 1
    @Jeff That is a terrible solution. The application should of course be able to run with normal user privileges. And you do already know how simple that would be to implement. – Clemens Jul 11 '23 at 15:55

1 Answers1

0

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
Demon
  • 379
  • 2
  • 7