본문 바로가기

C#

C# 특정 폴더내에서 xlsx 확장자를 가진 파일 모두 찾기(GetFiles)

반응형

특정 폴더내에서 xlsx 확장자를 가진 파일 모두 찾기

using System;
using System.IO;

class Program
{
    static void Main()
    {
        // 검색할 폴더 경로를 지정
        string folderPath = @"C:\your\folder\path";

        // Directory.GetFiles 메소드를 사용하여 .xlsx 파일을 검색
        // 이 메소드는 지정된 경로에서 특정 패턴에 맞는 파일 이름을 모두 반환
        string[] xlsxFiles = Directory.GetFiles(folderPath, "*.xlsx");

        // 찾은 파일들을 출력
        foreach (var file in xlsxFiles)
        {
            // 파일의 전체 경로 출력
            Console.WriteLine(file);
            
            // 파일의 전체 경로가 아닌 파일 이름만 출력하려면 Path.GetFileName 메소드를 사용
            Console.WriteLine(Path.GetFileName(file));
        }
    }
}

결과

 

반응형