파일 목록 구하기
C# 에서 폴더 안에 파일 목록 구하는 함수입니다.
public static ArrayList FindFiles(string CurPath, string Pattern, bool IsIncludeSubDir)
{
ArrayList DirAll = new ArrayList();
if (IsIncludeSubDir)
{
GetAllDirectories(CurPath, ref DirAll);
}
else
{
DirAll.Add(new DirectoryInfo(CurPath));
}
//파일의 목록
ArrayList aPathFiles = new ArrayList();
//앞에서 얻어온 모든 DirectoryInfo에 대한 루핑.
foreach (DirectoryInfo d in DirAll)
{
foreach (FileInfo fi in d.GetFiles())
{
aPathFiles.Add(fi.Name);
}
}
return aPathFiles;
}
/// <summary>
/// 지정한 폴더와 하위폴더에 대한 DirectoryInfo 개체를 배열에 저장함.
/// </summary>
/// <param name="Path">루트 경로</param>;
/// <param name="DirAll">각 폴더에 대한 DirectoryInfo를 저장한 배열</param>;
private static void GetAllDirectories(string Path, ref ArrayList DirAll)
{
DirectoryInfo di = new DirectoryInfo(Path);
DirAll.Add(di);
foreach (DirectoryInfo d in di.GetDirectories())
{
GetAllDirectories(d.FullName, ref DirAll);
}
}
파일 경로에서 파일명 추출
파일 경로 추출하는 방법을 소개합니다.
// 전체 경로
filesname = "D:\2014XXX.txt";
//전체 파일명 : 2014XXX.txt
fullName = filesname.Substring(filesname.LastIndexOf('\\') + 1);
//순수 파일명 : 2014XXX
name = fullName.Substring(0, fullName.LastIndexOf('.'));
//확장자 : .txt
ext = fullName.Substring(fullName.LastIndexOf('.'));