2011-09-28 66 views

回答

7

看看File.SetAttributes()。網上有很多關於如何使用它的例子。

從MSDN頁摘自:

FileAttributes attributes = File.GetAttributes(path); 

     if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) 
     { 
      // Show the file. 
      attributes = RemoveAttribute(attributes, FileAttributes.Hidden); 
      File.SetAttributes(path, attributes); 
      Console.WriteLine("The {0} file is no longer hidden.", path); 
     } 
     else 
     { 
      // Hide the file. 
      File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden); 
      Console.WriteLine("The {0} file is now hidden.", path); 
     } 
2

你忘了在方法的removeAttribute複製,這就是:

private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove) 
    { 
     return attributes & ~attributesToRemove; 
    } 
0

這是關於屬性(見JB的答案)或權限,即讀/寫訪問等?在後一種情況下,請參見File.SetAccessControl

從MSDN:

// Get a FileSecurity object that represents the 
// current security settings. 
FileSecurity fSecurity = File.GetAccessControl(fileName); 

// Add the FileSystemAccessRule to the security settings. 
fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType)); 

// Set the new access settings. 
File.SetAccessControl(fileName, fSecurity); 

How to grant full permission to a file created by my application for ALL users?的更具體的例子。

在原來的問題,它聽起來像你想要拒絕FileSystemRights.Delete權利。