2013-01-04 8 views
1

System.IO.FileInfo을 사용하지 않고 주어진 파일에서 '일부 속성'을 추출 할 수 있습니까?C# System.IO.FileInfo를 사용하지 않고 파일 속성 가져 오기

내 말은, 다루는 파일이 몇 개인 경우에 FileInfo을 사용하는 것을 좋아하지만, 예를 들어 파일 이름 목록을 가져 오려면 50 만 개의 파일과 I FileInfo ...을 사용하기로 결정했습니다.

나는 FileInfo이 그 파일에 관한 다른 것들을 메모리에로드한다고 의심한다. 그렇다면 이름이나 파일 확장자를 가져 오는 더 빠른 방법이 있어야하는 것 같습니다. 한편

, 난 그냥 Directory.GetFiles(myPath)를 사용할 수 있지만 난 그냥 파일 당 이름 필요 동안이 나에게 모든 파일에 대한 전체 경로 배열을 제공합니다! (나는 경로 문자열을 구문 분석하여 이름을 추출 할 수 있다고 생각합니다 ... FileInfo을 사용하는 것보다 빠르지는 않습니까?)

내가 수행하고 싶은 다른 대안은 무엇입니까? 파일을 받거나 파일 확장명을 잡아도됩니까?

대단히 감사합니다!

답변

2

Path 클래스의 메소드를 찾고 있습니다.

특히, Path.GetFileName()Path.GetExtension(). 그게 당신이에 관심이있는 모든 경우

+0

어머나! 번개 빠른 답변 주셔서 감사합니다! 나는 그것을 시도 할 것이다. – sergeidave

1

당신은 파일 이름을 얻을 수 Path.GetFilename을 사용할 수 있습니다. 귀하의 질문에 영감을

+0

감사합니다! 나는 우리가 말하는 것처럼 이것을 검사하고있다. – sergeidave

3

을, 나는에서는 FileInfo와 FileSystemObject를 비교했다. SSD 드라이브를 통과하는 데 걸리는 시간을 측정했습니다.

기본적으로 FileInfo는 FileSystemObject보다 약 3 배 빠릅니다.

test FileInfo Files: 489557 Directories: 66023 
FileInfo traversal needed 12,07s 

test FileSystemObject. Files: 489557 Directories: 66023 
FileInfo traversal needed 38,59s 

윈도우 API를 사용하여 시도 할 worthwile 수 있습니다 :

나는 지배 아웃 캐싱 효과를 내 시스템의 측정을 반복했다. 그러나 호출 및 매개 변수 전달은 마샬링 (Marshalling)으로 인해 성능 비용을 지불해야합니다.

자란 C 유틸리티는 SSD를 스캔하는 데 약 8 초가 필요합니다.

코드 :

using System; 
using System.Linq; 

using Scripting; 
using System.Diagnostics; 
using System.IO; 

namespace akTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Stopwatch watch = new Stopwatch(); 

      watch.Start(); 
      testFileInfo(@"c:\"); 
      watch.Stop(); 
      o("FileInfo traversal needed " + (watch.ElapsedMilliseconds/1000.0).ToString("#.##") + "s"); 

      watch.Start(); 
      testFSO(@"c:\"); 
      watch.Stop(); 
      o("FileInfo traversal needed " + (watch.ElapsedMilliseconds/1000.0).ToString("#.##") + "s"); 

      o(""); 
      o("Ciao!"); 
     } 

     static void o(string s) 
     { 
      Console.WriteLine(s); 
     } 

     static void testFileInfo(string dir) 
     { 
      DirectoryInfo di = new DirectoryInfo(dir); 
      long fileCount = 0; 
      long dirCount = 0; 
      long calls = 0; 

      o("Testing FileInfo"); 

      WalkDirectoryTree(di, ref fileCount, ref dirCount, ref calls); 

      o("testFileInfo completed. Files: " + fileCount + " Directories: " + dirCount + " Calls: " + calls); 
     } 

     static void testFSO(string dir) 
     { 
      FileSystemObject fso = new FileSystemObject(); 
      Folder folder = fso.GetFolder(dir); 

      long fileCount = 0; 
      long dirCount = 0; 
      long calls = 0; 

      o("Testing FSO"); 

      WalkDirectoryTree(folder, ref fileCount, ref dirCount, ref calls); 

      o("testFSO completed. Files: " + fileCount + " Directories: " + dirCount + " Calls: " + calls); 
     } 

     static void WalkDirectoryTree(DirectoryInfo root, ref long fileCount, ref long dirCount, ref long calls) 
     { 
      FileInfo[] files = null; 
      DirectoryInfo[] subDirs = null; 

      if (++calls % 10000 == 0) 
       o("" + calls); 

      try 
      { 
       files = root.GetFiles("*.*"); 

       if (files != null) 
       { 
        fileCount += files.Count(); 
        subDirs = root.GetDirectories(); 
        dirCount += subDirs.Count(); 

        foreach (DirectoryInfo dirInfo in subDirs) 
        { 
         WalkDirectoryTree(dirInfo, ref fileCount, ref dirCount, ref calls); 
        } 
       } 
      } 
      catch (Exception) 
      { 
      } 
     } 


     static void WalkDirectoryTree(Folder root, ref long fileCount, ref long dirCount, ref long calls) 
     { 
      Files files = null; 
      Folders subDirs = null; 

      if (++calls % 10000 == 0) 
       o("" + calls); 

      try 
      { 
       files = root.Files; 

       if (files != null) 
       { 
        fileCount += files.Count; 
        subDirs = root.SubFolders; 
        dirCount += subDirs.Count; 

        foreach (Folder fd in subDirs) 
        { 
         WalkDirectoryTree(fd, ref fileCount, ref dirCount, ref calls); 
        } 
       } 
      } 
      catch (Exception) 
      { 
      } 
     } 
    } 
} 
+0

이것은 매우 흥미로운 정보입니다. 고맙습니다! 나는 안으로 잠수 할 것이다. – sergeidave