프로그래밍/C#
c# - 폴더에 있는 파일이름 가져오기
HSDY
2018. 3. 14. 12:15
반응형
선택한 폴더에 있는 파일 이름을 출력하는 예제입니다.
상단에 using System.IO를 선언해주셔야 합니다. 아니면 System.IO.directoryInfo 이런 식으로 객체 생성할 때 앞에 붙여 주셔야 합니다.
파일 이름을 출력하는 코드는 아래와 같습니다.
using System.IO;
//폴더 경로를 입력
string Path= "C:\TEST";
//해당 폴더가 존재하는지 확인
if (System.IO.Directory.Exists(Path))
{
//DirectoryInfo 객체 생성
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Path);
//해당 폴더에 있는 파일이름을 출력
foreach (var item in di.GetFiles())
{
Console.WriteLine(item.Name);
}
}