코드 숨김으로 C#을 사용하는 WPF 응용 프로그램을 작성 중이며 사용자에게 응용 프로그램의 특정 설정을 변경할 수있는 옵션을 제공하려고합니다. 끊임없이 읽고 쓸 수있는 응용 프로그램 내에 설정을 저장하는 표준이 있습니까?WPF 응용 프로그램 설정 파일
1
A
답변
3
app.config
파일을 쓸 수 있지만 (쓰기 위해 열려면 ConfigurationManager.OpenExeConfiguration
을 사용) 실제로는 읽기 전용 설정을 거기에 저장하는 것이 좋습니다.
그것은 간단한 설정 클래스 쓰기 쉽다 : ConfigurationSection
클래스/저장 구성 파일에서 설정을 검색 할 수
public sealed class Settings
{
private readonly string _filename;
private readonly XmlDocument _doc = new XmlDocument();
private const string emptyFile =
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
<appSettings>
<add key=""defaultkey"" value=""123"" />
<add key=""anotherkey"" value=""abc"" />
</appSettings>
</configuration>";
public Settings(string path, string filename)
{
// strip any trailing backslashes...
while (path.Length > 0 && path.EndsWith("\\"))
{
path = path.Remove(path.Length - 1, 1);
}
_filename = Path.Combine(path, filename);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (!File.Exists(_filename))
{
// Create it...
_doc.LoadXml(emptyFile);
_doc.Save(_filename);
}
else
{
_doc.Load(_filename);
}
}
/// <summary>
/// Retrieve a value by name.
/// Returns the supplied DefaultValue if not found.
/// </summary>
public string Get(string key, string defaultValue)
{
XmlNode node = _doc.SelectSingleNode("configuration/appSettings/add[@key='" + key + "']");
if (node == null)
{
return defaultValue;
}
return node.Attributes["value"].Value ?? defaultValue;
}
/// <summary>
/// Write a config value by key
/// </summary>
public void Set(string key, string value)
{
XmlNode node = _doc.SelectSingleNode("configuration/appSettings/add[@key='" + key + "']");
if (node != null)
{
node.Attributes["value"].Value = value;
_doc.Save(_filename);
}
}
}
0
사용을
참조 : How to: Create Custom Configuration Sections Using ConfigurationSection
public class ColorElement : ConfigurationElement
{
[ConfigurationProperty("background", DefaultValue = "FFFFFF", IsRequired = true)]
[StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)]
public String Background
{
get
{
return (String)this["background"];
}
set
{
this["background"] = value;
}
}
}
0
창을 시도해 볼 수도 있습니다 \ 페이지의 XAML 리소스 섹션을 참조하십시오.
관련 문제
- 1. . Winform 응용 프로그램, 모범 사례의 설정 파일
- 2. 공유 Windows 응용 프로그램 설정 파일 (* .settings)?
- 3. 응용 프로그램 간의 설정 공유
- 4. WPF 응용 프로그램 확장
- 5. 설정 응용 프로그램에서 응용 프로그램 설정 만들기
- 6. 응용 프로그램 시간 설정
- 7. 응용 프로그램 설정 저장
- 8. .NET - 응용 프로그램 설정
- 9. .Net 응용 프로그램 설정
- 10. 응용 프로그램 설정 그룹화
- 11. WPF/Surface 응용 프로그램 내에서 WPF/Surface 응용 프로그램 호스팅
- 12. 데이터베이스 WPF 응용 프로그램
- 13. 웹용 WPF 응용 프로그램
- 14. WPF 응용 프로그램 스레드
- 15. Citrix의 WPF 응용 프로그램
- 16. WPF 응용 프로그램 레이아웃
- 17. WPF 응용 프로그램
- 18. wpf 빙지도 응용 프로그램?
- 19. WPF 샘플 응용 프로그램
- 20. WPF - 반투명 응용 프로그램
- 21. ASP에 WPF 응용 프로그램
- 22. WPF 응용 프로그램 동결
- 23. 응용 프로그램 설정 저장 | 레지스트리
- 24. 단일 파일 응용 프로그램 빌드
- 25. WPF 응용 프로그램 템플릿 2010
- 26. WPF 브라우저 응용 프로그램 디버깅
- 27. WPF 내장 데이터베이스 응용 프로그램
- 28. 외부 창에서 WPF 응용 프로그램 페이지 설정 속성
- 29. WPF DataGrid 설정 응용 프로그램 수준에서 선택된 행 색상
- 30. WPF Prism 4 모듈에서 사용자 응용 프로그램 설정 액세스