Visual Studio C#으로 Windows Forms, Console, Class Library and Windows Service Applications 등을 개발 시 App.config를 사용하여 환경설정 관리를 하는 방법
1. System.Configuration 참조 추가.(이미 참조 되어 있다면 패스)
기본적으로 Visual Studio에는 System.configuration이 참조에 들어가 있지 않아 코딩 시 정상적으로 안되는 경우가 있습니다.
아래이와 같이 참조에 추가합니다. (더보기)
2. AppConfiguration 관련 소스
아래의 소스를 미리 만들어 놓고 사용하면 될듯하다.(아래에 shift + [view row] 클릭하면 줄번호 없이 나옵니다.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
namespace AppConfiguration | |
{ | |
public class AppConfiguration | |
{ | |
public static string GetAppConfig(string key) | |
{ | |
return ConfigurationManager.AppSettings[key]; | |
} | |
public static void SetAppConfig(string key, string value) | |
{ | |
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); | |
KeyValueConfigurationCollection cfgCollection = config.AppSettings.Settings; | |
cfgCollection.Remove(key); | |
cfgCollection.Add(key, value); | |
config.Save(ConfigurationSaveMode.Modified); | |
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name); | |
} | |
public static void AddAppConfig(string key, string value) | |
{ | |
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); | |
KeyValueConfigurationCollection cfgCollection = config.AppSettings.Settings; | |
cfgCollection.Add(key, value); | |
config.Save(ConfigurationSaveMode.Modified); | |
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name); | |
} | |
public static void RemoveAppConfig(string key) | |
{ | |
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); | |
KeyValueConfigurationCollection cfgCollection = config.AppSettings.Settings; | |
try | |
{ | |
cfgCollection.Remove(key); | |
config.Save(ConfigurationSaveMode.Modified); | |
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name); | |
} | |
catch { } | |
} | |
} | |
} |
Win form 적용 예
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void button1_Click(object sender, EventArgs e) | |
{ | |
textBox2.Text = AppConfiguration.GetAppConfig(textBox1.Text); | |
} | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
AppConfiguration.AddAppConfig(textBox1.Text, textBox2.Text); | |
} | |
private void button3_Click(object sender, EventArgs e) | |
{ | |
AppConfiguration.SetAppConfig(textBox1.Text, textBox2.Text); | |
} | |
3. AppConfiguration 관련 적용 예
참고로 Config 파일은 exe 실행파일 폴더에 실행파일명.exe.Config 파일로 생성이 됩니다.
GetAppConfig 메소드의 경우 해당 Key 값에 저장된 Value 를 호출한다.
<사용 예>
AddAppConfig 의 경우 Value 값이 계속 늘어나는 형태이다.
<사용예>
SetAppConfig의 경우 기존 Value 값을 초기화하고 새로운 값을 입력한다.
<사용예>
'개발언어 > C#' 카테고리의 다른 글
주식&코인 차트 분석 및 자동매매 프로그램 개발 #1 - 고려사항 (0) | 2023.09.14 |
---|---|
텔레그램 봇 생성 및 C#을 이용한 간단한 챗봇 로직 작성하기 (0) | 2023.07.06 |
C# .Net 5.0 단일 실행 파일 생성 (1) | 2021.08.23 |
C #에서 싱글 톤 패턴 구현 (0) | 2020.01.17 |
Login Form 호출 예제 (0) | 2016.07.19 |