2014-12-18 2 views
0

동일한 솔루션으로 웹 응용 프로그램과 Windows 응용 프로그램이 있습니다. web.config 파일에 동적으로 연결 문자열을 추가하고 싶습니다. 연결 문자열 정보는 Windows 응용 프로그램에서 제공됩니다. 내가 어떻게 도와 줘? asp.net이 동적으로 web.config에 연결 문자열을 추가합니다.

내 윈도우 응용 프로그램 갖는

WebForm1 wf = new WebForm1(); 
wf.add(); 

그리고 내 WEP 응용 프로그램이 갖는

public void add() 
{ 
     Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
     ConnectionStringsSection sec = (ConnectionStringsSection)config.GetSection("connectionStrings"); 
     sec.ConnectionStrings["DBCS"].ConnectionString = "Data Source=GKS_004-PC;Database=hello1;User ID=123;Password=123"; 
     config.Save();   
    } 
+0

http://www.codeproject.com/Questions/217366/dynamically-set-connection-string-in-web-config http://www.aspsnippets.com/Articles/Programmatically-Add-or-Update-Connection-String-in-ASPNet-WebConfig-File.aspx – Vivekh

+0

이런 식으로 생각해보십시오. (실제로) "동적 인"경우, 왜 설정 파일에 저장해야합니까? 더 중요한 것은 설정 파일을 수정할 때마다 응용 프로그램이 다시 시작된다는 것입니다. – EdSF

답변

0

난 당신이 문제를 해결하기 위해 다른 접근 방식을 존재할 수있는 방식으로 문제를 해결 믿는다, 그러나 당신이 원하는 것을하기 위해서 당신에게는 두 가지 옵션이 있습니다. 먼저 IO 네임 스페이스를 사용하여 파일을 읽은 다음 LINQ 노드를 사용하여 XML과 같이 구문 분석하고 두 번째로 Configuration 클래스 (System.ConfigurationSystem.Web.Configuration 네임 스페이스)를 사용할 수 있습니다. 대신 직접 Configuration 클래스를 사용

//get the configuration file 
Configuration config = WebConfigurationManager.OpenWebConfiguration("..."); //path to config 

//get Configuration section 
ConfigurationSection section = config.GetSection("..."); 

config.AppSettings.Settings.Add(Key, Value); 
config.AppSettings.Settings.Remove(Key); 

또는 이러한 방법으로,

AppSettingsSection appsettings = (AppSettingsSection) config.GetSection("..."); 
appsettings.Settings.Add(key, Value); 
관련 문제