0

ユーザーが C# コンソール アプリケーションの設定を簡単な方法で変更できるようにする方法を探しています。キーと値のペアを提示して、値を変更できるようにしたいだけです。

私は、より多くの情報がユーザーに提示される解決策を見つけることしかできませんでした。彼らを混乱させるかもしれないこと、または私が彼らに変えてほしくないこと。

4

3 に答える 3

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

その後、xml ファイルとの間で保存/読み込みを行うことができます。

于 2013-03-18T13:35:39.230 に答える
0

独自の構成ファイルを使用してみてください。ユーザーがメモ帳でそのファイルを開いて設定を変更できるようにしてください。それ以外の場合は、それらにインターフェイスを提供できます。アプリケーションディレクトリのYourAppName.configのようなもの。

于 2013-03-18T13:30:30.993 に答える
0

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);
于 2013-03-18T13:45:49.517 に答える