[英]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.