C # .NET에서 App.config는 무엇입니까? 사용 방법?
데이터베이스 파일이 Excel 통합 문서 인 C # .NET에서 프로젝트를 수행했습니다. 연결 문자열의 위치는 코딩에 하드 코딩되어 있으므로 시스템에 설치하는 데 문제가 없지만 다른 시스템에는 문제가 있습니다.
응용 프로그램 설정이 완료된 후 사용자에게 경로를 한 번 설정하라는 메시지가 표시됩니까?
내가 얻은 대답은 "App.Config 사용"이었습니다 ... 누구나이 App.config가 무엇이며 여기에서 내 컨텍스트에서 사용하는 방법을 말할 수 있습니까?
가장 간단하게 app.config는 사전 정의 된 많은 구성 섹션이 있고 사용자 정의 구성 섹션을 지원하는 XML 파일입니다. "구성 섹션"은 일부 유형의 정보를 저장하기위한 스키마가 포함 된 XML 스 니펫입니다.
connectionStrings
또는 과 같은 내장 구성 섹션을 사용하여 설정을 구성 할 수 있습니다 appSettings
. 사용자 정의 구성 섹션을 추가 할 수 있습니다. 이것은 고급 주제이지만 강력한 형식의 구성 파일을 작성하는 데 매우 강력합니다.
웹 응용 프로그램에는 일반적으로 web.config가 있고 Windows GUI / 서비스 응용 프로그램에는 app.config 파일이 있습니다.
응용 프로그램 수준 구성 파일은 전역 구성 파일 (예 : machine.config)의 설정을 상속합니다.
App.Config에서 읽기
연결 문자열에는 사용할 수있는 미리 정의 된 스키마가 있습니다. 이 작은 스 니펫은 실제로 유효한 app.config (또는 web.config) 파일입니다.
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyKey"
connectionString="Data Source=localhost;Initial Catalog=ABC;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
app.config를 정의한 후에는 ConfigurationManager 클래스를 사용하여 코드에서 읽을 수 있습니다 . 장황한 MSDN 예제에 겁 먹지 마십시오. 실제로는 매우 간단합니다.
string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;
App.Config에 쓰기
* .config 파일을 자주 변경하는 것은 좋은 생각이 아니지만 일회성 설정 만 수행하려는 것처럼 들립니다.
런타임에 * .config 파일 의 섹션 을 업데이트하는 방법을 설명하는 런타임시 연결 문자열 변경 및 app.config 다시로드를 참조하십시오 connectionStrings
.
이상적인 설치 프로그램에서 이러한 구성 변경을 수행하는 것이 이상적입니다.
런타임시 App.Config의 위치
Q : <value>
app.config에서 일부 를 수동으로 변경하고 저장 한 다음 닫습니다. 이제 bin 폴더로 이동하여 여기에서 .exe 파일을 시작할 때 적용된 변경 사항이 반영되지 않는 이유는 무엇입니까?
A : 응용 프로그램을 컴파일하면 해당 app.config가 exe와 일치하는 이름으로 bin 디렉토리 1에 복사됩니다 . 예를 들어, exe의 이름이 "test.exe"인 경우 bin 디렉토리에 "text.exe.config"가 있어야합니다. 재 컴파일하지 않고 구성을 변경할 수 있지만 원래 app.config가 아니라 컴파일 타임에 작성된 구성 파일을 편집해야합니다.
1 : web.config 파일은 이동되지 않지만 컴파일 및 배포시 동일한 위치에 유지됩니다. 이에 대한 한 가지 예외는 web.config가 변환 될 때 입니다.
.NET 코어
.NET Core에는 새로운 구성 옵션이 도입되었습니다. * .config 파일의 작동 방식은 변경되지 않았지만 개발자는 새롭고 유연한 구성 패러다임을 자유롭게 선택할 수 있습니다.
간단히 말해 App.config 는 응용 프로그램 수준 구성XML
을 보유 하는 기반 파일 형식입니다 .
예:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="key" value="test" />
</appSettings>
</configuration>
ConfigurationManager
아래 코드 스 니펫에 표시된대로 사용하여 구성에 액세스 할 수 있습니다 .
var value = System.Configuration.ConfigurationManager.AppSettings["key"];
// value is now "test"
참고 : ConfigurationSettings
구성 정보를 검색하는 데 사용되지 않는 방법입니다.
var value = System.Configuration.ConfigurationSettings.AppSettings["key"];
Just to add something I was missing from all the answers - even if it seems to be silly and obvious as soon as you know:
The file has to be named "App.config" or "app.config" and can be located in your project at the same level as e.g. Program.cs.
I do not know if other locations are possible, other names (like application.conf, as suggested in the ODP.net documentation) did not work for me.
PS. I started with Visual Studio Code and created a new project with "dotnet new". No configuration file is created in this case, I am sure there are other cases. PPS. You may need to add a nuget package to be able to read the config file, in case of .NET CORE it would be "dotnet add package System.Configuration.ConfigurationManager --version 4.5.0"
App.Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.
See this MSDN article on how to do that.
You can access keys in the App.Config using:
ConfigurationSettings.AppSettings["KeyName"]
Take alook at this Thread
참고URL : https://stackoverflow.com/questions/13043530/what-is-app-config-in-c-net-how-to-use-it
'Programming' 카테고리의 다른 글
'for'루프에서 마지막 요소를 감지하는 pythonic 방법은 무엇입니까? (0) | 2020.06.03 |
---|---|
HAXM으로 OS X v10.9 (Mavericks)를 고정하는 Android 에뮬레이터 (0) | 2020.06.03 |
Agda와 Idris의 차이점 (0) | 2020.06.03 |
웹 브라우저에서 GPS 위치 확인 (0) | 2020.06.03 |
Java 7 JVM에서 실행되도록 Java 8 코드를 컴파일 할 수 있습니까? (0) | 2020.06.03 |