안녕하세요 모든 하위 디렉토리를 비롯하여 특정 기준과 일치하는 경로 디렉토리에서 모든 파일을 가져 오는 다음 코드가 있습니다. 현재 저는 두 가지 방법이 있습니다. 하나는 모든 파일을 가져 와서 목록에 추가 한 다음 다른 방법으로 목록을 반환합니다. 이것이 최선의 방법인지 궁금하거나 두 가지를 결합하는 것이 더 효율적입니까? 아니면 내 코드가 더 효율적으로 재 작성 될 수 있습니까? 미안해,이 모든 일에 아주 새로운거야!여러 폴더를 통해 파일 목록을 반환하는 가장 효율적인 방법
public class FileUtility
{
List<FileInfo> fileInfoList = new List<FileInfo>();
public void ProcessDir(string sourceDir, String userName)
{
try
{
string userNameFirstLetter = userName.First().ToString();
DirectoryInfo di = new DirectoryInfo(sourceDir);
foreach (FileInfo fi in di.GetFiles())
{
if (fi.Extension == ".xls" || fi.Extension == ".xlsx" || fi.Extension == ".pdf")
{
if (fi.Name.Contains(userName))
{
if (fi.Name.Contains("X"))
{
if(fi.Name.First().ToString().Equals(userNameFirstLetter))
{
if (fi.Name.Split(Convert.ToChar("X"))[0].Equals(userName))
{
fileInfoList.Add(fi);
}
}
}
}
}
}
// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(sourceDir);
foreach (string subdir in subdirEntries)
{
// Do not iterate through reparse points
if ((File.GetAttributes(subdir) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
{
ProcessDir(subdir, userName);
}
}
}
catch (DirectoryNotFoundException exp)
{
throw new DirectoryNotFoundException("Directory not found " + exp.Message);
}
catch (IOException exp)
{
throw new IOException("The Process cannot access the file because it is in use by another process " + exp.Message);
}
}
public List<FileInfo> GetFileInfoList()
{
return fileInfoList;
}
}
이것은 훨씬 효율적입니다 :) 감사합니다! –
@Chris - 투표에 사람들을 괴롭히지 마십시오 ... –