2008-09-18 156 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++;      
      }