상세 컨텐츠

본문 제목

Path

C#

by 메타 스토리 2023. 9. 4. 23:15

본문

           

            using System.IO;

 

 

            //GetFileName: 전체 경로에서 파일 이름만 추출합니다.
            string fileName = Path.GetFileName(@"C:\Users\John\Documents\example.txt");
            // 결과: "example.txt"

            //GetDirectoryName: 파일 경로에서 디렉토리 경로를 추출합니다.
            string directory = Path.GetDirectoryName(@"C:\Users\John\Documents\example.txt");
            // 결과: "C:\Users\John\Documents"

            //GetExtension: 파일 경로에서 확장자를 추출합니다.
            string extension = Path.GetExtension(@"C:\Users\John\Documents\example.txt");
            // 결과: ".txt"

            //Combine: 경로 요소를 결합하여 새 경로를 생성합니다.
            string fullPath = Path.Combine(@"C:\Users", "John", "Documents", "example.txt");
            // 결과: "C:\Users\John\Documents\example.txt"

            //GetFullPath: 상대 경로를 절대 경로로 변환합니다.
            string absolutePath = Path.GetFullPath("..\..\example.txt");
            // 결과: 현재 작업 디렉토리를 기준으로 한 절대 경로

            //IsPathRooted: 주어진 경로가 절대 경로인지 확인합니다.
            bool isAbsolute = Path.IsPathRooted(@"C:\Users\John\Documents\example.txt");
            // 결과: true

            //ChangeExtension: 파일 경로의 확장자를 변경합니다.
            string newFilePath = Path.ChangeExtension(@"C:\Users\John\Documents\example.txt", ".csv");
            // 결과: "C:\Users\John\Documents\example.csv"

            //GetTempFileName: 임시 파일 이름을 생성합니다.
            string tempFileName = Path.GetTempFileName();
            // 결과: 임시 파일의 경로

            //GetTempPath: 시스템의 임시 폴더 경로를 반환합니다.
            string tempFolderPath = Path.GetTempPath();
            // 결과: 시스템의 임시 폴더 경로

            //HasExtension: 주어진 경로에 확장자가 있는지 확인합니다.
            bool hasExtension = Path.HasExtension(@"C:\Users\John\Documents\example.txt");
            // 결과: true

'C#' 카테고리의 다른 글

Stack  (0) 2023.09.05
Queue  (0) 2023.09.05
List  (0) 2023.09.05
string.Format  (0) 2023.09.05
문자열  (0) 2023.09.05

관련글 더보기