ASP.NET에서 구성 파일 읽기 및 쓰기에 대한 설명

ringa_lee
풀어 주다: 2017-10-15 10:09:08
원래의
1543명이 탐색했습니다.

일반적으로 .NET 개발 과정에서 우리는 구성 파일과 xml 파일이라는 두 가지 유형의 구성 파일을 접하게 됩니다. 다음 문서에서는 주로 ASP.NET에서 구성 파일 읽기 및 쓰기에 대한 관련 정보를 소개합니다. 기사에서는 예제를 사용합니다. 코드가 매우 자세하게 소개되어 있습니다. 필요한 친구들은 참고할 수 있습니다.

이 글에서는 주로 ASP.NET의 Config 읽기 및 쓰기 예제에 대한 관련 내용을 소개하고 참고 및 학습을 위해 공유합니다. 더 이상 고민하지 말고 자세한 소개를 살펴보겠습니다.

방법은 다음과 같습니다.

WinForm 프로그램인 경우 참조를 추가해야 합니다.

  • System.ServiceModel

  • System.Configuration

App.config


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
 <add key="testkey" value="0"></add>
 </appSettings>
</configuration>
로그인 후 복사

NetUtilityLib


using System.Configuration;
namespace pcauto
{
 public static class ConfigHelper
 { 
  ///<summary>  
  ///返回*.exe.config文件中appSettings配置节的value项  
  ///</summary>  
  ///<param name="strKey"></param>  
  ///<returns></returns> 
  public static string GetAppConfig(string strKey)
  {
   string file = System.Windows.Forms.Application.ExecutablePath;
   Configuration config = ConfigurationManager.OpenExeConfiguration(file); 
   foreach (string key in config.AppSettings.Settings.AllKeys) { 
    if (key == strKey) { 
     return config.AppSettings.Settings[strKey].Value.ToString(); 
    } 
   }
   return null;
  }
  ///<summary>  
  ///在*.exe.config文件中appSettings配置节增加一对键值对  
  ///</summary> 
  ///<param name="newKey"></param> 
  ///<param name="newValue"></param> 
  public static void UpdateAppConfig(string newKey, string newValue) { 
   string file = System.Windows.Forms.Application.ExecutablePath;
   Configuration config = ConfigurationManager.OpenExeConfiguration(file); 
   bool exist = false; 
   foreach (string key in config.AppSettings.Settings.AllKeys) { 
    if (key == newKey) { exist = true; } 
   } 
   if (exist) { config.AppSettings.Settings.Remove(newKey); }
   config.AppSettings.Settings.Add(newKey, newValue); 
   config.Save(ConfigurationSaveMode.Modified);
   ConfigurationManager.RefreshSection("appSettings");
  }  
 }
}
로그인 후 복사

예제 읽기


ConfigHelper.GetAppConfig("testkey")
로그인 후 복사

예제 작성


아아아아

위 내용은 ASP.NET에서 구성 파일 읽기 및 쓰기에 대한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!