2017-02-24 1 views
1

여러 번 전에 비슷한 질문을 한 적이 있습니다. 나는 몇 가지 해답을 읽었지만 내 문제에 대한 명확한 답을 찾지 못했다.C# 다른 응용 프로그램의 구성 파일을 어떻게 수정하고 변경 사항을 저장합니까?

namespace ModifyOtherConfig 
{ 
    public partial class Form1 : Form 
    { 
     string otherConfigFilePath; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); 
      fileMap.ExeConfigFilename = @"c:\users\om606\documents\visual studio 2015\projects\csharptesting\csharptesting\bin\debug\csharptesting.exe"; 
      Configuration otherConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 


      string otherSetting = otherConfig.AppSettings.Settings["Key0"].Value; 
      MessageBox.Show(otherSetting); 
      otherSetting = "098"; 
      MessageBox.Show(otherSetting); 
      otherConfig.SaveAs(fileMap.ExeConfigFilename, ConfigurationSaveMode.Full); 
     } 
    } 
} 

:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key = "Key0" value = "4567" /> 
    <add key = "Key1" value = "1" /> 
    <add key = "Key2" value = "2" /> 
    </appSettings> 
</configuration> 

응용 프로그램 B는 앱의 "쪽에 KEY0에게"A 구성 파일을 수정하려고 : 지점에, 나는이 응용 프로그램은 다음과 같이 & B. 응용 프로그램 A는 구성 파일이 말한다 은 RO에서 데이터 형식 'System.Configuration.ConfigurationErrorsException'의

처리되지 않은 예외 System.Configuration.dll에 추가 정보를 발생 :이 나는 다음과 같은 오류가 코드를 실행하려고 레벨이 유효하지 않습니다. 1 호선, 1 번 위치.

내가 뭘 잘못 했니? 나는 아주 명백한 것을 놓치고 있니? 누군가가 올바른 방향으로 나를 가리킬 수 있다면 고맙겠습니다.

+0

파일에 올바른 xml이없는 것 같습니다. – Jonesopolis

답변

2

오, fileMap.ExeConfigFilename.exe으로 지정하고 대신 .config 파일을 가리 키도록 변경하십시오. 그것이 XML 오류를 보는 이유입니다. 다른 문제에 대한

fileMap.ExeConfigFilename = @"c:\users\om606\documents\visual studio 2015\projects\csharptesting\csharptesting\bin\debug\csharptesting.exe.config"; 

는 수행

otherConfig.AppSettings.Settings.Remove("Key0"); 
otherConfig.AppSettings.Settings.Add("Key0", "098"); 

후 저장합니다.

+0

실제로 "csharptesting.exe.config"가 아니어야합니다. – Kevin

+0

예. 좋은 전화. 그냥 .config 파일로 끝나는 파일을 가리킨다. darnit – Jonesopolis

+0

좋아, .config 파일을 추가하면 오류는 없지만 "Key0"app 구성 파일에는 "098"값이 저장되지 않는다. 아이디어? 어쨌든 위의 감사합니다. – cott

관련 문제