✅ 방법 : 빌드 구성별로 서로 다른 App.config
파일 사용 (고급)
Visual Studio에서 빌드 시 구성별로 다른 App.config
를 복사하도록 설정할 수 있습니다.
🔹 Step 1: 별도 구성 파일 생성
프로젝트에 아래 두 파일을 추가:
App.Debug.config
App.Release.config
내용 예시:
App.Debug.config
1 2 3 4 5 6 7 |
<configuration> <appSettings> <add key="ApiUrl" value="http://localhost/api" /> </appSettings> </configuration> |
App.Release.config
1 2 3 4 5 6 7 |
<configuration> <appSettings> <add key="ApiUrl" value="https://prodserver/api" /> </appSettings> </configuration> |
🔹 Step 2: .csproj
에 조건부 복사 설정 (수동 또는 .targets
파일 사용)
Visual Studio에서 직접 변경하려면:
- 프로젝트 우클릭 → “언로드(Unload) 프로젝트”
- 다시 우클릭 → “편집”
- 아래 내용 추가:

1 2 3 4 5 6 7 8 9 |
<PropertyGroup Condition="'$(Configuration)' == 'Debug'"> <AppConfig>App.Debug.config</AppConfig> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)' == 'Release'"> <AppConfig>App.Release.config</AppConfig> </PropertyGroup> |
- 저장 후 프로젝트 다시 로드
이렇게 하면 빌드할 때 해당 구성에 맞는 config 파일이 App.config
로 자동 복사됩니다.
사용방법
✅ 1.코드에서 불러오기 (.NET Framework 4.6.1 콘솔 앱 예시)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.Configuration; class Program { static void Main() { string pdfPath = ConfigurationManager.AppSettings["PdfPath"]; Console.WriteLine("PDF 경로: " + pdfPath); } } |
✅ 2. const
대신 readonly
사용하기
const
는 컴파일 타임 상수여서 외부 파일 값을 사용할 수 없습니다. 대신 readonly
로 선언하세요:
1 2 3 |
private static readonly string PdfPath = ConfigurationManager.AppSettings["PdfPath"]; |
✅ 3. 전체 예시 클래스
1 2 3 4 5 6 7 8 9 |
using System; using System.Configuration; public static class Config { public static readonly string PdfPath = ConfigurationManager.AppSettings["PdfPath"]; } |
사용
1 2 3 |
Console.WriteLine(Config.PdfPath); |
🔹 참고 C# 코드에서 조건 분기 처리
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Configuration; class Program { static void Main() { #if DEBUG string apiUrl = ConfigurationManager.AppSettings["ApiUrl_Debug"]; #else string apiUrl = ConfigurationManager.AppSettings["ApiUrl_Release"]; #endif Console.WriteLine("API URL: " + apiUrl); } } |