2011-09-01 5 views
1

로컬 app.config 파일에서 값을 읽는 exe가 있습니다.로컬 app.config에서 값 업데이트

TargetDate = ConfigurationManager.AppSettings.Get("ThresholdDate"); 

// and try to update with the current date 
ConfigurationManager.AppSettings.Set("ThresholdDate", "2011-09-01"); 

한 번 작동했지만 app.config가 전혀 업데이트되지 않는 것을보고 있습니다.

+1

이 유용한 정보 : http://stackoverflow.com/questions/980440/update-app-config-system-net-setting-at-runtime – Baz1nga

답변

2

난 당신이 뭔가를 시도 할 수 있다고 생각 제거의 조합을하고 다음과 같이 추가 할 수 있습니다

config.AppSettings.Settings.Remove("ThresholdDate"); 
config.AppSettings.Settings.Add("ThresholdDate", "2011-09-01"); 
+0

구성 값을 변경하기위한 C# 구문은 무엇입니까? – user360943

+0

제거를 시도하고 추가 ... 흠, 물건을 바꾸지 않았다 – user360943

+0

그 후에 저장하고 새로 고침을 했습니까? – Baz1nga

2

여기에 상대 : How to change App.config file run time using C#는 대답은 - 비주얼 스튜디오 IDE에서 실행하는 동안, 당신은 볼 수 없습니다 값은 /bin/appname.exe에 지속 .config. 실제로 bin 디렉토리로 가서 exe를 실행해야합니다.

그래서이 코드는 실제로 그냥 디버그 모드에서 작동합니다

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

config.AppSettings.Settings["ThresholdDate"].Value = Convert.ToString(testdate); 
config.Save(ConfigurationSaveMode.Modified); 
ConfigurationManager.RefreshSection("appSettings"); 
+2

문제가 해결 된 경우이를 수락 된 답변으로 표시해야합니다 (현재 48 시간 타이머가 만료되어야 함). – Matthieu

-1
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

config.AppSettings.Settings["ThresholdDate"].Value = Convert.ToString(testdate); 
config.Save(ConfigurationSaveMode.Modified); 
ConfigurationManager.RefreshSection("appSettings"); 

이 당신의하지에 debuging 모드에서에만 작동합니다. /debug/appName.exe에서 yung 코드를 실행하면 appName.exe.config에 변경 사항이 표시됩니다.

건배!