AppDomain.CurrentDomain.BaseDirectory로 얻은 디렉토리 경로에 /Data/CreateDB.sql 파일의 경로를 추가하려면, Path.Combine() 메서드를 사용하여 경로를 안전하게 결합할 수 있습니다.
다음은 currentDirectory에 /Data/CreateDB.sql 파일의 경로를 결합하고 이를 currentFilename 변수에 저장하는 코드 예시입니다:
using System; using System.IO; class Program { static void Main(string[] args) { // 현재 실행 중인 프로그램의 경로 (dotnet run 실행 폴더) string currentDirectory = AppDomain.CurrentDomain.BaseDirectory; // 하위 디렉토리와 파일 경로를 결합 string currentFilename = Path.Combine(currentDirectory, "Data", "CreateDB.sql"); // 결과 출력 Console.WriteLine($"현재 파일 경로: {currentFilename}"); } } |
설명:
- AppDomain.CurrentDomain.BaseDirectory: 현재 실행 중인 프로그램의 경로를 반환합니다.
- Path.Combine(): 여러 경로 요소를 결합하여 올바른 경로 문자열을 생성합니다. 이 방법은 운영 체제에 맞는 경로 구분자를 자동으로 사용하므로 \ 또는 /를 직접 처리할 필요가 없습니다.
- "Data"는 BaseDirectory의 하위 디렉터리입니다.
- "CreateDB.sql"은 그 디렉터리 내의 파일입니다.
결과:
- 위 코드에서 currentFilename은 currentDirectory 경로와 Data/CreateDB.sql을 결합한 절대 경로를 가지게 됩니다. 예를 들어, currentDirectory가 C:\MyApp\인 경우, currentFilename은 C:\MyApp\Data\CreateDB.sql이 됩니다.
참고:
Path.Combine()은 각 부분을 안전하게 결합하여 운영 체제에 맞는 경로를 생성하므로, 경로 구분자를 직접 추가하거나 처리할 필요가 없어 더 안전하고 권장되는 방법입니다.
class Program { public static void Main() { CHKNM nmm = new CHKNM(); nmm.RUN(); } } class CHKNM { public void RUN() { string currentDirectory = AppDomain.CurrentDomain.BaseDirectory; // 하위 디렉토리와 파일 경로를 결합 string currentFilename = Path.Combine(currentDirectory, "Data", "CreateDB.sql"); string currentEstFilename = Path.Combine(currentDirectory, "Data", "CreateEstateDB.sql"); Console.WriteLine(currentDirectory); Console.WriteLine(currentFilename); Console.WriteLine(currentEstFilename); } } |
C:\Users\xterm\Downloads\dotnetconsole90>dotnet run C:\Users\xterm\Downloads\dotnetconsole90\dotnetconsole90.csproj : warning NU1803: 'http://nuget.grapecity.com/nuget' 'HTTP' 원본을 사용하여 'restore' 작업을 실 행하고 있습니다. 비 HTTPS 액세스는 이후 버전에서 제거됩니다. 'HTTPS' 원본으로 마이그레이션하는 것이 좋습니다. C:\Users\xterm\Downloads\dotnetconsole90\dotnetconsole90.csproj : warning NU1803: 'http://nuget.grapecity.com/nuget' 'HTTP' 원본을 사용하여 'restore' 작업을 실 행하고 있습니다. 비 HTTPS 액세스는 이후 버전에서 제거됩니다. 'HTTPS' 원본으로 마이그레이션하는 것이 좋습니다. C:\Users\xterm\Downloads\dotnetconsole90\bin\Debug\net8.0\ C:\Users\xterm\Downloads\dotnetconsole90\bin\Debug\net8.0\Data\CreateDB.sql C:\Users\xterm\Downloads\dotnetconsole90\bin\Debug\net8.0\Data\CreateEstateDB.sql C:\Users\xterm\Downloads\dotnetconsole90> |
'c# 언어 > 중급과정' 카테고리의 다른 글
배열 인덱스 범위는 [startIndex..endIndex) 형식이므로, 0..5는 0부터 4까지, 총 5개의 바이트를 포함합니다. (0) | 2025.01.21 |
---|---|
HashSet<T>의 Add 메서드는 중복된 값을 허용하지 않습니다. (0) | 2025.01.21 |
Json 구조체.출력형태 코드(따로따로, 혹은 전체) (0) | 2025.01.16 |
Visual Studio에서 app.manifest 파일 생성 및 설정 방법 (0) | 2025.01.15 |
C#의 Queue<T>는 기본적으로 동적 크기로 결정됩니다. (0) | 2025.01.09 |