2017-08-15 40 views
-2

我在这里从CodeProject,但他们似乎没有我的问题的答案。我只想让桌面文件具有安全权限(使文件不可移动,可复制或可删除)。这是可能的手动在文件的“属性!我正在使用C#,您的帮助非常感谢 - 谢谢!使文件不可移动,可复制或可删除?

作为助手下面说,如果它是不可能的,使一个可读的文件不可复制...是否有可能使它不能移动和删除?

+3

有没有办法让一个可读的文件不可复制。 – SLaks

+0

将文件夹权限设置为只列出文件夹内容,但这对于此处也不是问题 – Sorceri

+0

我不确定将所需文件放在_desktop_上是一个好设计。有更好的地方放置文件。桌面上的_shortcut_可能是可以接受的,但不是文件本身。 –

回答

0

除了不可复制之外,所有这些都是可能的。除非您限制系统上的所有复制(这是一项艰巨的任务),否则始终可以复制可读文件。防止它被移动或删除

从文件权限的角度来看,您需要将所有者设置为管理员,然后向相关用户授予只读权限。让他们阅读文件但不删除或移动它。

这样做很简单,使用中的FileInfo.SetAccessControl方法; here's the documentation to help out

编辑添加例如,信贷MS的例子:

using System; 
using System.IO; 
using System.Security.AccessControl; 

namespace FileSystemExample 
{ 
    class FileExample 
    { 
     public static void Main() 
     { 
      try 
      { 
       string FileName = "c:/test.txt"; 

       Console.WriteLine("Adding access control entry for " + FileName); 

       // Add the access control entry to the file. 
       // Before compiling this snippet, change MyDomain to your 
       // domain name and MyAccessAccount to the name 
       // you use to access your domain. 
       AddFileSecurity(FileName, @"MyDomain\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow); 

       Console.WriteLine("Removing access control entry from " + FileName); 

       // Remove the access control entry from the file. 
       // Before compiling this snippet, change MyDomain to your 
       // domain name and MyAccessAccount to the name 
       // you use to access your domain. 
       RemoveFileSecurity(FileName, @"MyDomain\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow); 

       Console.WriteLine("Done."); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e); 
      } 

     } 

     // Adds an ACL entry on the specified file for the specified account. 
     public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) 
     { 
      // Create a new FileInfo object. 
      FileInfo fInfo = new FileInfo(FileName); 

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

      // Add the FileSystemAccessRule to the security settings. 
      fSecurity.AddAccessRule(new FileSystemAccessRule(Account, 
                  Rights, 
                  ControlType)); 

      // Set the new access settings. 
      fInfo.SetAccessControl(fSecurity); 

     } 

     // Removes an ACL entry on the specified file for the specified account. 
     public static void RemoveFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) 
     { 
      // Create a new FileInfo object. 
      FileInfo fInfo = new FileInfo(FileName); 

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

      // Add the FileSystemAccessRule to the security settings. 
      fSecurity.RemoveAccessRule(new FileSystemAccessRule(Account, 
                  Rights, 
                  ControlType)); 

      // Set the new access settings. 
      fInfo.SetAccessControl(fSecurity); 

     } 
    } 
}