2017-03-02 4 views
1

연결 문자열을 만들고 연결 문자열을 편집 할 수있는 응용 프로그램을 만들었습니다.이전 연결 문자열 삭제

doc.Load(Path.Combine(path, SelectConfigComboBox.SelectedItem.ToString(), "app.config")); 
XmlNode xNode = doc.CreateNode(XmlNodeType.Element, "add", ""); 
XmlAttribute xName = doc.CreateAttribute("name"); 
XmlAttribute xconnectionString = doc.CreateAttribute("connectionString"); 
xName.Value = NewKeyTextBox.Text; 

xconnectionString.Value = string.Format("data source={0};persist security info={1};initial catalog={2};USER ID={3};password={4}", NewValueTextBox.Text, SecurityInfocomboBox.Text, CatalogcomboBox.Text, UserIDtextBox.Text, PasswordtextBox.Text); 

xNode.Attributes.Append(xName); 
xNode.Attributes.Append(xconnectionString); 
doc.GetElementsByTagName("connectionStrings")[0].InsertAfter(xNode, 
doc.GetElementsByTagName("connectionStrings")[0].LastChild); 

doc.Save(Path.Combine(path, SelectConfigComboBox.SelectedItem.ToString(), "app.config")); 

이 코드에서는 새 connectionString을 입력합니다. 연결 문자열을 편집하려면 다른 연결 문자열을 삭제하지 않고 새 연결 문자열을 추가합니다. 새 항목을 추가하기 전에 어떻게 이전 항목을 삭제할 수 있습니까?

답변

1

"ConfigurationManager"를 사용하여 app.config에 액세스하고 삭제하는 대신 기존 값을 업데이트 할 수 있습니다. 다음 예제 코드를 참조하십시오.

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
config.AppSettings.Settings["test"].Value = "blah";  
config.Save(ConfigurationSaveMode.Modified); 
ConfigurationManager.RefreshSection("appSettings"); 
+0

답변 해 주셔서 감사합니다.이 코드를 doc.load와 doc.save 사이에 추가해야합니까? –

+0

아니요, 위의 예제 코드를 (코드없이) 사용해보십시오. 도움이되기를 바랍니다. – imsome1

+0

귀하의 코드를 시도했지만 구성 _ 유형 또는 네임 스페이스 이름 "구성"을 찾을 수 없습니다 각 줄 아래에 빨간색 줄을 제공합니다 –