0

I have an app in which the user needs to access certain files in a user set and selected folder. The folder and files paths need to be easily accessed (short simple path). I use the Properties Settings to hold the Folder and File paths, but for some reason each time I re-start the program the Folder and File paths are lost. I have followed and checked the program and all seems to be OK (except something I am missing, apparently). I attach here the program snippet in two parts: The search for path and the setting in case path / file not found. (removed exception handling to save on lines)

     public Main()   //part of Main, stripped off exception handling)
    {
        //..........

        dataFolder = Properties.Settings.Default.dataFolder;
        if (!Directory.Exists(dataFolder))
        {
            SetDataFolder();
        }
        configFile = Properties.Settings.Default.configFile;
        if (!File.Exists(configFile))
        {
            SetConfigFile();
        }

        dataFile = Properties.Settings.Default.dataFile;
        if (!File.Exists(dataFile))
        {
            SetDataFile();
        }
        
        loadParamsFromFile(configFile);  //Load the previously saved controls.

      public String SetDataFolder()
    {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            DialogResult folder = dialog.ShowDialog();
            if (folder == DialogResult.OK)
            {
                dataFolder = dialog.SelectedPath;
                Directory.CreateDirectory(dataFolder);
                dataFolder = Path.GetFullPath(dataFolder);
                Properties.Settings.Default.dataFolder = dataFolder;
                Properties.Settings.Default.Save();
                return dataFolder;
            }
            else return null;
      }
    private string SetDataFile()
    {
            dataFile = $"{dataFolder}\\{textBoxSampleID.Text.Replace("/r", "").Trim()}.txt";
            File.Create(dataFile).Close();
            Properties.Settings.Default.dataFile = dataFile;
            Properties.Settings.Default.Save();
            return dataFile;
     }

    private string SetConfigFile()
    {
        configFile = $"{dataFolder}\\electroplating.cfg";
        File.Create(configFile).Close();
        Properties.Settings.Default.configFile = configFile;
        Properties.Settings.Default.Save();
        return configFile;
    }
samtal
  • 131
  • 2
  • 12
  • Have you tried to simply store one value to the Properties settings and retrieve it? You are manipulating your paths quite a bit, can it be you have made a mistake there? Easiest is to Debug.Print your path at moment of storing and when you retrieve again to compare. – Aldert Oct 30 '21 at 07:38
  • How does one manage to select a nonexistent folder in a folderbrowserdialog? – Caius Jard Oct 30 '21 at 08:22
  • How do you know the settings are lost? – Caius Jard Oct 30 '21 at 08:24
  • Never used the Debug.Print. Will try it out. Properties setting should obviously save the data. It is clear to me that I make a mistake somewhere. Using registry seems to be a bad workaround to fix my errors. To Caius Jard: I did not write the folder does not exist. It just not loaded by the program from the settings file on startup. In addition, folderbrowserdialog has the "Add new folder" so that even if the folder is lost, one can build a new one (but, as I said, it is not lost). – samtal Oct 31 '21 at 06:46

1 Answers1

0

Check out this question: How to change application settings (Settings) while app is open?

I would suggest using Path.Combine() for the construction of the file paths.

If it still doesn't work, you could also try using the registry for storing the values.

string dataFilePath = Path.Combine(dataFolder, textBoxSampleID.Text.Replace("/r", "").Trim());
RegistryKey key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Company");
if (key != null)
{
    key.SetValue("dataFilePath", dataFilePath);
}

You could then use string dataFilePath = (string)key.GetValue("dataFilePath") to get the value out of the registry.

  • Thanks. I found out that the porblem with persisting only appears when I am in debug, mode, when the program exits by exception or by manually stopping debug. If I close the program in the correct manner I programmatically save the settings that are then loaded upon startup as expected. It is still a question why the settings are lost from previous savings to file, but I believe I can ignore it for normal user program operation. – samtal Oct 31 '21 at 06:56