2008-09-18 185 views
33

有沒有什麼辦法可以通過編程在c#中創建存儲設備上的隱藏文件夾?創建隱藏文件夾

+1

重新標記,因爲這不是對谷歌 – 2008-09-18 14:35:20

回答

84
using System.IO; 

string path = @"c:\folders\newfolder"; // or whatever 
if (!Directory.Exists(path)) 
{ 
DirectoryInfo di = Directory.CreateDirectory(path); 
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden; 
} 
+8

第一個結果現在你是谷歌的第一個結果。 – 2008-09-18 13:12:31

+7

一個C#語言特定的問題 – KDecker 2015-08-25 19:11:26

24

是的,你可以。照常創建目錄,然後在其上設置屬性。例如。

DirectoryInfo di = new DirectoryInfo(@"C:\SomeDirectory"); 

//See if directory has hidden flag, if not, make hidden 
if ((di.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) 
{ 
    //Add Hidden flag  
    di.Attributes |= FileAttributes.Hidden;  
} 
+0

如果子句可以簡化爲`if(!di.Attributes.HasFlag(FileAttributes.Hidden))` – schoetbi 2017-04-05 12:24:52

4
string path = @"c:\folders\newfolder"; // or whatever 
if (!System.IO.Directory.Exists(path)) 
{ 
    DirectoryInfo di = Directory.CreateDirectory(path); 
    di.Attributes = FileAttributes.Directory | FileAttributes.Hidden; 
} 

here

6
CreateHiddenFolder(string name) 
{ 
    DirectoryInfo di = new DirectoryInfo(name); 
    di.Create(); 
    di.Attributes |= FileAttributes.Hidden; 
} 
-2

僅獲取根文件夾路徑的代碼。

一樣,如果我們有C:/測試/ C:/測試/ ABC C:/測試/ XYZ C:/ Test2的/ C:/ Test2的/ MNP

它將返回根文件夾路徑即 C:/測試/ C:/ Test2的/

  int index = 0; 
      while (index < lst.Count) 
      { 
       My obj = lst[index]; 
       lst.RemoveAll(a => a.Path.StartsWith(obj.Path)); 
       lst.Insert(index, obj); 
       index++;      
      }