2011-09-28 14 views

Odpowiedz

7

Spójrz na File.SetAttributes(). Istnieje wiele przykładów online, jak z niego korzystać.

pobranych z tej strony 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

zapomniałeś skopiować w metodzie removeAttribute, który jest:

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

Chodzi o atrybutach (zob. Odpowiedź JB) lub uprawnienia , np. dostęp do odczytu/zapisu itp.? W tym drugim przypadku patrz File.SetAccessControl.

Od 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); 

Zobacz How to grant full permission to a file created by my application for ALL users? dla bardziej konkretny przykład.

W oryginalnym pytaniu brzmi to tak, jakby chcieć odrzucić prawo FileSystemRights.Delete.

Powiązane problemy