2008-09-18 34 views
33

Có cách nào để tôi có thể tạo các thư mục ẩn (và tôi đoán truy cập) theo lập trình trên thiết bị lưu trữ từ trong C#?Tạo các thư mục ẩn

+1

retagged, vì đây không phải là một ngôn ngữ C# cụ thể câu hỏi –

Trả lời

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

kết quả đầu tiên trên google –

+7

Bây giờ bạn là kết quả đầu tiên trên Google. – KDecker

24

Có thể. Tạo thư mục như bình thường sau đó chỉ cần thiết lập các thuộc tính trên nó. Ví dụ.

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

Nếu mệnh đề có thể được giảm xuống thành 'if (! Di.Attributes.HasFlag (FileAttributes.Hidden))' – schoetbi

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; 
} 

Từ here.

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

Mã để chỉ nhận đường dẫn thư mục gốc.

Giống như Nếu chúng ta có C:/Kiểm tra/ C:/Kiểm tra/Abc C:/Kiểm tra/xyz C:/Test2/ C:/Test2 path/MNP

Nó sẽ trở lại thư mục gốc tức là C:/Kiểm tra/ 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++;      
      } 
+6

Đoạn mã mà không giải thích thì hiếm khi hữu ích. Vui lòng thêm một số ngữ cảnh cho điều này. (Ngoài ra, bạn có thể quan tâm để biết rằng bạn đã trả lời bài đăng trên 6 tuổi ...) – Chris

Các vấn đề liên quan