1

I have a Winform program I wrote for a university so they could run test files. The program is targeted at .NET 4.5 and was developed and run on Windows 7.

To make the running of files easy for the technicians I added the ability to save the paths to the test files. The file path is stored in a settings file (ProgSettings.settings) under the variable "RunNames".

When the save button is pressed I run a validation to ensure the test file at the end of the path exists and is valid before adding it to the RunNames list:

    /*
    *Function: SaveButton_Click(sender, e)
    *Notes: responds to the button click "Save"
    *       tries to save file (if valid) to settings
    */
    private void SaveButton_Click(object sender, EventArgs e)
    {
        if (ValidateFile(TrackFileName.Text))
        {
            string displayName = updateLinkNames(TrackFileName.Text).ToUpper();
            if (!ListOfTracks.Items.Contains(displayName))
            {
                ListOfTracks.Items.Add(displayName);
                runNames.Add(TrackFileName.Text);
            }
            updateSettings();
        }
    }

    /*
    *Function: ValidateFile(fileName)
    *Notes: called from save button click
    *       checks validity of the file path
    */
    private bool ValidateFile(string fileName)
    {
        fileName = fileName.ToUpper();
        FileToRun.FileName = fileName;
        validFile = System.IO.File.Exists(fileName);
        if(fileName != null && fileName != "" && fileName.EndsWith(".TXT") && validFile)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /*
    *Function: updateSettings()
    *Notes: updates the settings with the valid path
    */
    private void updateSettings()
    {
        string value = String.Join(",", runNames.Select(i => i.ToString()).ToArray());
        ProgSettings.Default.RunNames = value;
        ProgSettings.Default.Save();
    }

(For those of you unfamiliar with .settings they store the basic types available on C#, there are ways to include arrays as well but the simplest solution is to concatenate the different parts of the array in a string with a separator - in my case ",").

Then when I load the program again I populate a list of checkboxes using the a getSavedFiles() function below.

   private List<string> getSavedFiles()
   {
         string localRunNames = ProgSettings.Default.RunNames.ToUpper();
         string[] arr = localRunNames.Split(',').Select(s => Convert.ToString(s)).ToArray();
         return arr.Cast<String>().ToList();
   }

The settings works correctly on both my work computer and my personal laptop I did testing on. However, now the program is in use at the university they are saying that after shutting down the computer the next time the run the program all the saved file paths are gone.

Is this due to how to how the university managers users accounts on the computer? Or is there some other reason for this?

Was there another alternative to storing setting information for cases where .settings files don't work? (The only other solution I can think of is having the program write a config.txt file which I would prefer not to do encase the application is used somewhere it won't having writing permissions)

Below is a link to a question about the best practice to save application settings:

Community
  • 1
  • 1
Luke
  • 1,077
  • 13
  • 33
  • Hmm, doubtful. Everybody thinks ".txt" is a valid filename extension, you don't. You'd better also check what happens when Environment.CurrentDirectory changes. – Hans Passant Jul 02 '15 at 23:59
  • Can you try changing `ValidateFile` to `private bool ValidateFile(string fileName) { fileName = fileName.ToUpper(); FileToRun.FileName = fileName; return System.IO.File.Exists(fileName); }` and see if that helps? – Enigmativity Jul 03 '15 at 00:02
  • 3
    Probably not your issue, but I recommend you use the pipe delimiter `|`, not a comma. Commas are legit in windows filenames and can mess up your parsing. The pipe char is not allowed in file/folder names. – DeanOC Jul 03 '15 at 00:07
  • The code that you have posted works fine, but I couldn't test your `FileToRun` or `ProgSettings` code. Can you please post the source for them? – Enigmativity Jul 03 '15 at 00:32
  • 1
    Are different techs using this app under distinct user accounts? – David W Jul 03 '15 at 00:33
  • @Enigmativity - ValidateFile(string fileName) is not the issue as the file name is being added to the "ListOfTracks" which is a check box list. Also on my personal PC it is there whether I shut the program or restart the computer. The FileToRun is a winform editable text box and the ProgSettings is a windows settings file (ProgSettings.settings) – Luke Jul 03 '15 at 01:23
  • @DeanOC - you are right, this is not my issue but would be better practice to use a "|" or other illegitimate name character. I will change now. – Luke Jul 03 '15 at 01:24
  • @DavidW - currently there is one operator of the program/tests. He logs in on the same PC with the same account. He is quite an old school mechanical engineer and isn't interested much in new (electronic) technology so will have to go myself to find out where on the PC is putting the program (he has decided to run from the .exe rather than use the installer I created) – Luke Jul 03 '15 at 01:26
  • In my .settings file the scope of my variables is currently set to user NOT application. I think this might fix the issue if he has but in the communal C: drive on the unversity PC if I change it to "Application" scope? – Luke Jul 03 '15 at 01:28

0 Answers0