【问题标题】:Simple User configurable settings file简单的用户可配置设置文件
【发布时间】:2013-03-18 13:27:24
【问题描述】:

我正在寻找一种允许用户以简单的方式更改 C# 控制台应用程序设置的方法。我只想向他们展示一个键:值对,然后他们可以在其中更改值。

我只能找到解决方案,向用户提供更多信息。可能让他们感到困惑的事情或我不希望他们改变的事情。

【问题讨论】:

    标签: .net c#-4.0


    【解决方案1】:
    [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"});
    

    然后您可以保存/加载到和从 xml 文件。

    【讨论】:

    • 这种方法不仅限于 XML 文件(这可能不是 Mikkel 最初要求的),而是可以使用任何序列化程序(例如,请参阅 this post 了解更多选项。
    【解决方案2】:

    尝试使用您自己的配置文件,并让用户通过在记事本中打开该文件来更改设置。否则,您可以为它们提供接口。应用程序目录中的 YourAppName.config 之类的东西。

    【讨论】:

      【解决方案3】:

      如果您使用的是 applicaiton.exe.config,那么您可以使用与此类似的代码。

      在下面的代码示例中 - 您必须相应地进行更改

         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);
      

      【讨论】:

        猜你喜欢
        • 2017-09-25
        • 2022-01-10
        • 2023-04-06
        • 2014-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多