Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 简单的用户可配置设置文件_.net_C# 4.0 - Fatal编程技术网

.net 简单的用户可配置设置文件

.net 简单的用户可配置设置文件,.net,c#-4.0,.net,C# 4.0,我正在寻找一种方法,允许用户以简单的方式更改C#控制台应用程序的设置。我只想给他们一个key:value对,他们可以在这里更改值 我只能找到解决方案,向用户提供更多信息。可能使他们感到困惑的事情或我不希望他们更改的事情。尝试使用您自己的配置文件,并允许用户通过在记事本中打开该文件来更改设置。否则,您可以为它们提供接口。类似于应用程序目录中的YourAppName.config。[Serializable] [Serializable] public class SettingItem {

我正在寻找一种方法,允许用户以简单的方式更改C#控制台应用程序的设置。我只想给他们一个key:value对,他们可以在这里更改值


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

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

[Serializable]
[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"});
公共类设置项 { 公共字符串名称{get;set;} 公共字符串值{get;set;} } 私有列表项目设置=新列表(); 添加(新设置项{Name=“SomeKey”,“SomeValue”});

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

如果您使用的是applicationon.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);

这种方法不仅限于XML文件(可能不是Mikkel最初要求的),还可以使用任何序列化程序(例如,请参阅以获取更多选项)。