Program Tip

app.config에 값을 추가하고 검색합니다.

programtip 2020. 10. 27. 23:10
반응형

app.config에 값을 추가하고 검색합니다.


다음과 같이 app.Config에 키 값 쌍을 삽입해야합니다.

<configuration>
 <appSettings>
    <add key="Setting1" value="Value1" />
    <add key="Setting2" value="Value2" />
 </appSettings>
</configuration>

Google에서 검색했을 때 다음 코드 스 니펫을 얻었습니다.

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Add an Application Setting.

config.AppSettings.Settings.Add("ModificationDate",
               DateTime.Now.ToLongTimeString() + " ");

// Save the changes in App.config file.

config.Save(ConfigurationSaveMode.Modified);

위 코드는 .NET 2.0을 사용중인 System.Configuration 네임 스페이스에서 ConfigurationManager를 찾을 수 없기 때문에 작동하지 않습니다. 프로그래밍 방식으로 app.config에 키-값 쌍을 추가하고 검색하는 방법은 무엇입니까?


System.Configuration.dll에 대한 참조가 누락 되었습니까? ConfigurationManager수업이 거기에 있습니다.

편집 : System.Configuration네임 스페이스는 mscorlib.dll, system.dll 및 system.configuration.dll에 클래스가 있습니다. 프로젝트에는 항상 mscorlib.dll 및 system.dll 참조가 포함되지만 system.configuration.dll은 기본적으로 존재하지 않으므로 대부분의 프로젝트 유형에 추가해야합니다.


이것은 작동합니다 :

public static void AddValue(string key, string value)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    config.AppSettings.Settings.Add(key, value);
    config.Save(ConfigurationSaveMode.Minimal);
}

에 참조를 추가 System.Configuration하면 System 네임 스페이스를 참조하여 일부 구성 네임 스페이스를 가져오고 System.Configuration에 대한 참조를 추가하면 ConfigurationManager.


이것이 효과가 있기를 바랍니다.

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

config.AppSettings.Settings["Yourkey"].Value = "YourValue";
config.Save(ConfigurationSaveMode.Modified);

App.config에서 데이터를 가져 오려면

다음을 수행해야합니다.

  1. 참고 문헌에 추가-> System.Configuration
  2. 또한 이것을 using 문을 추가했습니다-> using System.Configuration;

그냥 이렇게

string value1 = ConfigurationManager.AppSettings["Value1"];

또는 using System.Configuration;명시 적으로 추가하지 않으려면 한 줄로이 작업을 수행 할 수 있습니다 .

string value1 = System.Configuration.ConfigurationManager.AppSettings["Value1"]

늦게 답변을 드려 죄송하지만 내 코드가 도움이 될 수 있습니다.

윈폼 표면에 3 개의 버튼을 배치했습니다. button1 & 2는 다른 값을 설정하고 button3은 현재 값을 검색합니다. 그래서 내 코드를 실행할 때 먼저 참조 System.configuration을 추가하십시오.

첫 번째 버튼을 클릭 한 다음 세 번째 버튼을 클릭하여 어떤 값이 설정되었는지 확인합니다. 다음에 다시 두 번째 및 세 번째 버튼을 클릭하여 변경 후 설정된 값을 다시 확인하십시오.

그래서 여기에 코드가 있습니다.

using System.Configuration;

 private void button1_Click(object sender, EventArgs e)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    config.AppSettings.Settings.Remove("DBServerName");
    config.AppSettings.Settings.Add("DBServerName", "FirstAddedValue1");
    config.Save(ConfigurationSaveMode.Modified);
}

private void button2_Click(object sender, EventArgs e)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    config.AppSettings.Settings.Remove("DBServerName");
    config.AppSettings.Settings.Add("DBServerName", "SecondAddedValue1");
    config.Save(ConfigurationSaveMode.Modified);
}

private void button3_Click(object sender, EventArgs e)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
          MessageBox.Show(config.AppSettings.Settings["DBServerName"].Value);
}

참고 URL : https://stackoverflow.com/questions/925638/add-values-to-app-config-and-retrieve-them

반응형