0

Im looking for a way to allow users to change settings for a C# console application, in a simple way. I only wanna present them with a key:value pair, where they can then change the value.

I have only been able to find solutions, where a lot more information is presented to the user. Things that might confuse them or things I don't want them to change.

Mikkel Nielsen
  • 792
  • 2
  • 13
  • 36

3 Answers3

2
[Serializable]
public class SettingItem
{
    public string Name { get; set; }
    public string Value { get; set; }
}

private List<SettingItem> ProjSettings = new List<SettingItem>();
ProjSettings.Add(new SettingItem {Name = "SomeKey", "SomeValue"});

You can then save/load the to and from xml files.

David
  • 15,894
  • 22
  • 55
  • 66
  • This approach will not be limited to just XML files (which might not be what Mikkel originally asked for), but any serializer can be used (e.g., see [this post](http://stackoverflow.com/questions/549128/fast-and-compact-object-serialization-in-net) for more options. – bigge Mar 18 '13 at 13:44
0

Try using your own config file, and let users change settings by opening that file in notepad. Otherwise, you can provide them interface. Something like YourAppName.config in application directory.

Yahya
  • 3,386
  • 3
  • 22
  • 40
0

If you are using , an applicaiton.exe.config then you can use something code similar to this .

In below code example - you have to make changes accordingly

   ArrayList keysArrList = new ArrayList();
            keysArrList.AddRange(hashConfigTable.Keys);
            keysArrList.Sort();
         //Get the application configuration file.
                    System.Configuration.Configuration config =
                                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                    if (filepath.Length > 0)
                    {
                        System.Configuration.ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
                        configFileMap.ExeConfigFilename = filepath;

                        config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
                    }


                ConfigurationSectionGroup mySectiongrp = config.GetSectionGroup("IF ANY GROUP PRESENT IN CONFIG FILE");
                ConfigurationSection mySection = mySectiongrp.Sections[sFormatClassName];



                foreach (Object okey in keysArrList)
                {
                    XmlNode node = GetNodeAvailable(document, okey.ToString());

                    XmlAttributeCollection attrcoll = node.Attributes;

                    foreach (XmlAttribute attr in attrcoll)
                    {                   
                        if ( String.Equals(attr.Name ,"VALUE",StringComparison.OrdinalIgnoreCase))
                        {
                            XmlComment newComment;
                            newComment = document.CreateComment(string.Format(" Modified by Batch WinConsole Version:{0} on Date:{1} PREVIOUS_VALUE:{2}  By:{3}", m_sFileVersion, DateTime.Now, attr.Value,System.Environment.UserName));

                            XmlElement element = attr.OwnerElement;
                            element.AppendChild(newComment);
                            attr.Value = Convert.ToString(hashConfigTable[okey]);
                        }
                    }
                }


    mySection.SectionInformation.SetRawXml(document.OuterXml);


                //Before save take a backup
                FileSystemUtil fsutil = new FileSystemUtil();
                string sNewfilename=string.Format("{0}_{1}.config",Path.GetFileNameWithoutExtension(filepath), DateTime.Now.ToString("yyyyMMMdd_hhmmss"));
                fsutil.FileCopy(filepath, Path.Combine(Path.GetDirectoryName(filepath), "Backup", "config", sNewfilename));


                //final Save
                config.Save(ConfigurationSaveMode.Full);
                ConfigurationManager.RefreshSection(sFormatClassName);
aked
  • 5,625
  • 2
  • 28
  • 33