사용자가 간단한 방법으로 C# 콘솔 응용 프로그램의 설정을 변경할 수있는 방법을 찾고 있습니다. 키 쌍으로 값을 표시하고 값을 변경할 수 있습니다.간단한 사용자 구성 설정 파일
나는 훨씬 더 많은 정보가 사용자에게 제공되는 솔루션을 찾을 수있었습니다. 자신이나 물건을 혼란스럽게 할 수있는 것들은 내가 바꾸기를 원하지 않습니다.
사용자가 간단한 방법으로 C# 콘솔 응용 프로그램의 설정을 변경할 수있는 방법을 찾고 있습니다. 키 쌍으로 값을 표시하고 값을 변경할 수 있습니다.간단한 사용자 구성 설정 파일
나는 훨씬 더 많은 정보가 사용자에게 제공되는 솔루션을 찾을 수있었습니다. 자신이나 물건을 혼란스럽게 할 수있는 것들은 내가 바꾸기를 원하지 않습니다.
[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 파일을 저장 /로드 할 수 있습니다.
자신의 설정 파일을 사용해보고 사용자가 메모장에서 파일을 열어 설정을 변경하도록하십시오. 그렇지 않으면 인터페이스를 제공 할 수 있습니다. 응용 프로그램 디렉토리의 YourAppName.config와 같은 것입니다.
applicaiton.exe.config를 사용하는 경우 이와 비슷한 코드를 사용할 수 있습니다. 아래 코드 예제에서
- 당신이 (Mikkel 원래 요구되지 않을 수 있습니다 무엇을) 따라
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 파일에 국한되지 않습니다 변경할 필요가 있지만, 어떤 시리얼 라이저를 사용할 수 있습니다 (예 : 자세한 옵션은 [this post] (http://stackoverflow.com/questions/549128/fast-and-compact-object-serialization-in-net)를 참조하십시오. – bigge