2011-08-20 58 views
2

我的目標是要檢查,如果DirectoryInfo.FullName是特殊文件夾中的一個。檢查DirectoryInfo.FullName是特殊的文件夾

下面是我在做什麼這個(支票directoryInfo.FullName每個特殊的文件夾,如果他們是平等的):

 DirectoryInfo directoryInfo = new DirectoryInfo("Directory path"); 

     if (directoryInfo.FullName == Environment.GetFolderPath(Environment.SpecialFolder.Windows) || 
      directoryInfo.FullName == Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles ||) 
      ... 
      ... 
      ) 
     { 
      // directoryInfo is the special folder 
     } 

但也有許多特殊文件夾(餅乾,的ApplicationData,InternetCache等。 )。有沒有辦法更有效地完成這項任務?

謝謝。

回答

4

試試這個下面的代碼:

 bool result = false; 
     DirectoryInfo directoryInfo = new DirectoryInfo("Directory path"); 
     foreach (Environment.SpecialFolder suit in Enum.GetValues(typeof(Environment.SpecialFolder))) 
     { 
      if (directoryInfo.FullName == Environment.GetFolderPath(suit)) 
      { 
       result = true; 
       break; 
      } 
     } 

     if (result) 
     { 
      // Do what ever you want 
     } 

希望這有助於。

1

恐怕給出的答案似乎是唯一的出路,我最討厭的特殊文件夾,因爲什麼應該是一個很簡單的功能 -

void CollectFiles(string strDir, string pattern) { 
    DirectoryInfo di = new DirectoryInfo(strDir); 
    foreach(FileInfo fi in di.GetFiles(pattern) { 
    //store file data 
    } 
    foreach(DirectoryInfo diInfo in di.GetDirectories()) { 
    CollectFiles(diInfo); 
    } 
} 

變爲醜陋的,因爲你必須包括

Check If This Is A Special Folder And Deal With It And Its Child Folders Differently(); 

不夠公平微軟,有可能在任何地方存在,在遠程PC,企業服務器等,但真的有什麼不對的UNIX/Linux的方法,使用鏈接到文件夾,如果目標物理文件夾中有一個文件夾移動,改變鏈接。然後你可以用一個漂亮整潔的功能對它們進行處理,把它們當作普通文件夾一樣處理。