2

I am very new to C# and using Visual Studio 2015 as my development environment. I have done my best to search for existing answers, but have not been able to find anything that seems relevant to my question.

I want to have application settings stored in config file (5 optional strings along with 5 booleans) Along with each string I want to store an associated boolean that indicates whether or not the optional value or default value should be used. I need to use these values in a method that replaces every instance of a certain string with the user specified string providing the boolean returns true and loops over each string flagged by a positive boolean.

I do not feel as if I have explained myself properly, so here are some basic rules that outline what I wish to implement.

  • Settings form should have 5 check boxes next to 5 text boxes.
  • For each check box marked true, input string from the associated text box should be stored in a config file.
  • Along with each stored string there needs to be a boolean value (obtained via check-box state)

My settings form looks something like the following.

The empty square brackets at the start of each line represents the check box and the square brackets with Xs in it ([xxxxxx]) represent the text boxes used to obtain user defined strings.

___________________________________________________________________________________________________
                                      SETTINGS FORM
         

         [ ]:use my own string for element 1       [xxxxxxx]: custom string 1
         [ ]:use my own string for element 2       [xxxxxxx]: custom string 2
         [ ]:use my own string for element 3       [xxxxxxx]: custom string 3
         [ ]:use my own string for element 4       [xxxxxxx]: custom string 4
         [ ]:use my own string for element 5       [xxxxxxx]: custom string 5


                                                            [Cancelbutton]              [Savebutton]
____________________________________________________________________________________________________

Im specifically interested in how I should go about storing user specified strings and associated boolean values in order to retrieve them considering the following requirements...

  • The method that uses these values should loop through each element...where the check-box is true it should replace a string from a data stream with the user defined string.
  • Config file should not be updated until the user presses the "Save" button
  • These settings need to persist until they are changed

I feel like I have done a poor job at explaining what I wish to achieve here, and am happy to provide further clarification as needed.

  • Is JSON an option? – mjwills Aug 23 '21 at 02:55
  • 1
    The most standard way to achieve this in the modern world and .net core is to use ConfigurationBuilder, however, if you just need to read a file at runtime, the absolutely easiest thing to do would be just read from a json file, deserialize the json. Example of configuration builder https://stackoverflow.com/questions/31453495/how-to-read-appsettings-values-from-a-json-file-in-asp-net-core – TheGeneral Aug 23 '21 at 02:58
  • If you're using WinForms, `Settings` file may be fit your need. https://learn.microsoft.com/vi-vn/dotnet/desktop/winforms/advanced/how-to-create-a-new-setting-at-design-time?view=netframeworkdesktop-4.8 – Le Vu Aug 23 '21 at 02:58
  • Using JSON is possible. If a JSON file is not present is it possible to create one - the way app.config file is implemented? – Randall Smith Aug 23 '21 at 03:06
  • Is the setting per user, or application wide? Do you want it editable within the program (if so, the config file is a poor choice)? Do you want people to edit this outside the program, or only within the program. For per-user settings that I don't want exposed to the user except through my program, I have a simple utility I bring in that stores app settings as an XML file in _Isolated Storage_ (which can be per app or per user/per app). You could do the same thing with JSON as well. My code is so old, XML was the thing to do back then – Flydog57 Aug 23 '21 at 03:09
  • The settings should be application wide. Ideally Id like the values to be editable within the application, however exposure of settings to user is not a problem if that method would prove more simple to implement. – Randall Smith Aug 23 '21 at 03:17
  • 1
    `var result = JsonConvert.Deserialize(File.ReadAllText("SomeFile.Json"))` – TheGeneral Aug 23 '21 at 03:27
  • 1
    It's important to note that `"SomeFile.json"` in @thegeneral's suggestion should not be in the application's install folder. Put it in _IsolatedStorage_ or in one of the *Data folders in `Environment.SpecialFolders`. Your app should **not** be writing in its install folder – Flydog57 Aug 23 '21 at 05:26
  • @Flydog57 good point, well said – TheGeneral Aug 23 '21 at 05:27

0 Answers0