-1

I have a list box and that list box contain multiple string value. I want to add those strings to config file and want to read values from config and put it in list box. what should be the approach? I am new to configuration file.

anonymous
  • 1
  • 2
  • 2
    What kind of app is this? Winforms, web, WPF, UWP..? – stuartd Jun 22 '16 at 16:11
  • I'm surprised that none ever done it before... Maybe try using search provided by company I work for https://www.bing.com/search?q=retrieve+string+values+from+configuration+file+in+c%23 (or one that I don't if you feel that your search history is safer somewhere else - https://www.google.com/?gws_rd=ssl#q=retrieve+string+values+from+configuration+file+in+c%23) – Alexei Levenkov Jun 22 '16 at 16:21
  • This is a windows form application which I am working on – anonymous Jun 22 '16 at 16:30
  • @AlexeiLevenkov, I thought you could only use Bing if you were 65+ and still on IE. – MZawg Mar 29 '19 at 14:49

3 Answers3

1

The following snippet will help you modify the app.config file of your project.

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

config.AppSettings.Settings.Remove("MySetting");
config.AppSettings.Settings.Add("MySetting", "some value");

config.Save(ConfigurationSaveMode.Modified);

For reading the config values, you can simply use ConfigurationManager.AppSettings["MySetting"]

Sujeet Sinha
  • 2,417
  • 2
  • 18
  • 27
  • Hi , Thank you very much for your prompt reply. I have string values like "New Order", Cancel order", "Route Order" , "Pending order". These values i want to add in app.config and I just want to read these values from config. and pass on to combobox. App.config file stores value in Key value pair where in i just have values to be stored. In future instead of making any code change I i need to add any new string i should be able to modify only config file. How do i do it?? – anonymous Jun 22 '16 at 16:35
  • In the `app.config`, you can add a custom string in the `appSettings` section. There you can store the values in a comma delimited or semicolon delimited format. Then you can simply split the string or append as and when needed, either programmatically or manually. – Sujeet Sinha Jun 22 '16 at 16:38
0

You can store and retrieve configuration data for your app via it's app.config file.

You'll need to add a reference to System.Configuration in your code and add the System.Configuration assembly to your application.

Here is a good post with more info - What is App.config in C#.NET? How to use it?

Community
  • 1
  • 1
totalfreakingnoob
  • 413
  • 1
  • 9
  • 25
0

probably you wanted to have those values present in a file and then read from that file and attach to your listbox like

    string path = @"c:\temp\MyTest.txt";

    if (File.Exists(path))
    {
        string[] readText = File.ReadAllLines(path);
    }

You can attach that your listbox

listbox1.DataSource = new List<string>().AddRange(readText);
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Any specific reason to create dependency on external resources? – Sujeet Sinha Jun 22 '16 at 16:17
  • Is your config file not an external resource as well. Though you access it through a class library class, you are kind a doing same thing (file read/write operation) – Rahul Jun 22 '16 at 16:20
  • I do agree on that, but creating a custom dependency when you have a built-in functionality in the framework will be a preferable approach. My understanding can be wrong.. Maybe you have some ideas which I can learn #HappyLearning – Sujeet Sinha Jun 22 '16 at 16:22
  • Hi rahul, instead of file I want to add those strings in app.config. and only i want to read those values from app.config and store in my combobox. How do i do it? – anonymous Jun 22 '16 at 16:31
  • @JaveriaNasir, you already have a answer then. See the other answer. – Rahul Jun 22 '16 at 16:33