简体   繁体   English

如何限制文件夹/子文件夹/文件仅访问程序?

[英]How to limit folder/sub folders/files access to a program only?

I am creating ac# winforms application. 我正在创建ac#winforms应用程序。

And I have a folder ScanRepository, and I want to prevent other programs and windows users to open this folder, sub folders and files, with a kind of password or a key, and to be able to open them in my c# code. 我有一个文件夹ScanRepository,我想防止其他程序和Windows用户使用一种密码或密钥来打开此文件夹,子文件夹和文件,并能够使用我的C#代码打开它们。

Any suggestion? 有什么建议吗?

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

private void btnBrowse_Click(object sender, EventArgs e)
{
   if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
   {
        textBox1.Text = folderBrowserDialog1.SelectedPath;
   }
}

private void btnLock_Click(object sender, EventArgs e)
{
   try
   {

      string folderPath = textBox1.Text;
      string adminUserName = Environment.UserName;// getting your adminUserName
      DirectorySecurity ds = Directory.GetAccessControl(folderPath);
      FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName,  FileSystemRights.FullControl, AccessControlType.Deny)

      ds.AddAccessRule(fsa);
      Directory.SetAccessControl(folderPath, ds);
      MessageBox.Show("Locked");
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }       
}

private void btnUnLock_Click(object sender, EventArgs e)
{
   try
   {
     string folderPath = textBox1.Text;
     string adminUserName = Environment.UserName;// getting your adminUserName
     DirectorySecurity ds = Directory.GetAccessControl(folderPath);
     FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName,  FileSystemRights.FullControl, AccessControlType.Deny)

     ds.RemoveAccessRule(fsa);
     Directory.SetAccessControl(folderPath, ds);
     MessageBox.Show("UnLocked");
   }
   catch (Exception ex)
   {
     MessageBox.Show(ex.Message);
   } 
}

The most obvious answer is that most things that let your user in, will let other apps in.. if you lock a folder to a user, while that folder is unlocked to that user other apps can see in too cos the user can see in. 最明显的答案是,让您的用户进入的大多数事物都将允许其他应用程序进入..如果您将文件夹锁定给用户,而该文件夹已解锁给该用户,则其他应用程序也可能会在用户看到的情况下看到该文件夹​​。 。

best way is to have a service running as an account that the user cant use. 最好的方法是让服务作为用户无法使用的帐户运行。 Then, you lock the access to only that account (and system, if you want backups of it), then you make a service that handles any access to those files and lists etc it interfaces with your app.. so in your users eyes they have access, in essense they dont. 然后,您只锁定对该帐户的访问权限(和系统,如果要备份该帐户),然后制作一个服务来处理对这些文件和列表等的访问,该服务与应用程序连接..因此,在您的用户眼中,他们有访问权,实质上他们没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何限制文件夹/文件只能访问程序? - How to limit folder/file access to a program only? 文件夹中可以存储的文件/文件夹是否有限制? - Is there a limit of files / folders that can be stored within a folder? Rackspace CloudFiles C#API:如何在根“文件夹”中列出文件,以及如何在“文件夹”中列出(子)文件夹? - Rackspace CloudFiles C# API: How to list files on root 'folder', and how to list (sub)folders within a 'folder'? 如何在子文件夹中获取文件 - how to get files in sub folders 如何显示所选文件夹中的子文件夹? - How to show sub folders present in a selected Folder? 如何读取仅在今天创建的子文件夹中的CSV文件 - How to read CSV files in sub folder which created today only 对Program Files文件夹的写访问权限 - Write Access to Program Files folder 需要访问“Program Files”文件夹 - Access to “Program Files” folder needed 从文件夹,其子文件夹及其中的所有文件中删除只读 - removing readonly from folder, its sub folders and all the files in it 如果子文件夹和它们本身不包含任何文件,如何删除没有任何文件的子文件夹然后删除父文件夹 - How to remove sub folders without any files then parent folders if their sub folders and themselves do not contain any files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM