2017-08-15 38 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); 

     } 
    } 
}