programming/C#

Json, Xml, Ini설정파일 읽기/쓰기

programmer j 2024. 10. 19. 02:50

NetworkTemplate.zip
0.03MB
NetworkTemplateWpfSetting.xml
0.00MB
NetworkTemplateWpfSetting.ini
0.00MB
NetworkTemplateWpfSetting.json
0.00MB

 

여러 형식으로 설정 파일을 읽고 쓰는 방법을 정리해 놓는다.

 

1. 프로젝트 만들기

- Visual Studio 2022 Version 17.11.4

- Target framework : .NET 8.0

 

2. 주요 nuget패키지

Newtonsoft.Json

PeanutButter.INI

 

3. 실행화면

 

4. Json, Xml, Ini설정 읽기/쓰기

- MainWindowViewModel.cs

- 설정 읽기

private void OnLoaded(object? obj)

{

    // Load settings from a json file

    _jsonSetting = JsonSettingHelper.LoadSetting<NetworkTemplateWpfSetting>(JsonSettingFileName);

    // Load settings from an xml file

    _xmlSetting = XmlSettingHelper.LoadSetting<NetworkTemplateWpfSetting>(XmlSettingFileName);

    // Load settings from an ini file

    _iniSetting = IniSettingHelper.LoadSetting(IniSettingFileName);

}

 

- 설정쓰기

private void OnJsonOptions()

{

              :

    // Save the setting to a json file with pretty print

    JsonSettingHelper.SaveSetting(JsonSettingFileName, _jsonSetting);

}

private void OnXmlOptions()

{

              :

    // Save the setting to an xml file

    XmlSettingHelper.SaveSetting(XmlSettingFileName, _xmlSetting);

}

private void OnIniOptions()

{

              :

    // Save the setting to an ini file

    IniSettingHelper.SaveSetting(IniSettingFileName, _iniSetting);

}

 

5. OptionsWindow에서 각 탭의 ViewViewModel연결

- OptionsWindow.xaml

TabItemViewViewModel연결

 

참고자료

- 로그설정 설명

https://programmer-jeong.tistory.com/27

- CommunityToolkit.Mvvm사용시 다이얼로그창 표시방법1

https://programmer-jeong.tistory.com/29