简体   繁体   English

禁用复制/粘贴到特定文件夹 - C#

[英]Disable Copy/Paste into a specific folder - C#

I have this C# program:我有这个 C# 程序:

TestProgram:
        static void Main(string[] args)
                {
                    var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);            
                    var directory = Directory.CreateDirectory(Path.Combine(path, "folder"));
                    using (StreamWriter sw = new StreamWriter(Path.Combine(directory.FullName, "example.txt")))
                    {
                        sw.WriteLine("hi");
                    }
                    var directorySecurity = directory.GetAccessControl();
                    var administratorRule = new FileSystemAccessRule("Administrators", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
                    var usersRole = new FileSystemAccessRule("Users", FileSystemRights.Read, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
                    directorySecurity.AddAccessRule(administratorRule);
                    directorySecurity.AddAccessRule(usersRole);
                    directory.SetAccessControl(directorySecurity);            
                }

So, I want to create a directory which only Administrators can edit the name and the content.所以,我想创建一个只有Administrators可以编辑名称和内容的目录。

In order to test this code snippet, I've copied this directory into C: and I've created a usertest which is member of Users group and stop.为了测试这个代码片段,我已经将此目录复制到C:并且我创建了一个Users组成员的usertest并停止。 This works fine because usertest cannot edit example.txt .这很好用,因为usertest无法编辑example.txt But if I (as usertest ) create a test.txt file on C:\Users\usertest\Desktop and copy/paste into C:\folder created by the execution of TestProgram.exe , the operation is successful.但是,如果我(作为usertest )在C:\Users\usertest\Desktop上创建一个test.txt文件并将其复制/粘贴到C:\folder执行TestProgram.exe创建的文件夹中,则操作成功。 How can I edit the C# program in order to avoid the copy/paste into folder ?如何编辑 C# 程序以避免复制/粘贴到folder中?

Before you ask, I've already search in StackOverflow but nothing seems to be right for my context.在您问之前,我已经在 StackOverflow 中进行了搜索,但似乎没有什么适合我的上下文。

Thanks谢谢

EDIT: on C: folder, the Users group ( usertest is a member of it) has the:编辑:在C:文件夹中, Users组( usertest是其中的成员)具有:

  • Read and execute读取并执行
  • Read
  • List folder content列出文件夹内容

rights.权利。 So, how can I inhibit the inheritance from C: to folder ?那么,如何抑制 inheritance 从C:folder

To stop inheritance in Windows 11:要在 Windows 11 中停止 inheritance:

Windows explorer -> Right click on the folder -> Select the menu option named "Properties" -> Change to the tab named "Security" -> Click the button named "Advanced Settings" -> Hit the button at the bottom named "Disable inheritance". Windows 资源管理器 -> 右键单击文件夹 -> Select 菜单选项名为“属性” -> 切换到名为“安全”的选项卡 -> 单击名为“高级设置”的按钮 -> 点击底部名为“禁用”的按钮遗产”。

The Advanced Settings option has a really useful tab named "Effective access" where you can see what can actually do users or groups to that object.高级设置选项有一个非常有用的选项卡,名为“有效访问”,您可以在其中查看用户或组对 object 的实际操作。

If you want to do it using c#, you can call DirectorySecurity.SetAccessRuleProtection() and make the bool preserveInheritance to false.如果您想使用 c# 执行此操作,您可以调用DirectorySecurity.SetAccessRuleProtection()并将bool preserveInheritance设置为 false。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM