2011-09-28 147 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权利。