简体   繁体   English

如何在C#中更改System32中文件夹的权限?

[英]How to change permissions of a folder inside System32 in C#?

I need to programmatically remove "create file" and "create directory" permissions on a directory inside System32 directory for a group "NT AUTHORITY\\\\INTERACTIVE" . 我需要以编程方式删除"NT AUTHORITY\\\\INTERACTIVE"组在System32目录内某个目录上的“创建文件”和“创建目录”权限。

To do this, I wrote following code: 为此,我编写了以下代码:

        string windir = Environment.GetEnvironmentVariable("systemroot");
        string redirectionFolder = (windir + "\\System32\\Tasks2");
        MessageBox.Show(redirectionFolder);
        FileSystemAccessRule Tasks = new FileSystemAccessRule("NT AUTHORITY\\INTERACTIVE", FileSystemRights.CreateDirectories | FileSystemRights.CreateFiles, AccessControlType.Deny );
        DirectorySecurity dirSecurity = new DirectorySecurity(redirectionFolder, AccessControlSections.Group);
        dirSecurity.AddAccessRule(Tasks);
        Directory.SetAccessControl(redirectionFolder, dirSecurity);

When I run this code on a folder C:\\Tasks2 , it works. 当我在文件夹C:\\Tasks2上运行此代码时,它可以工作。

But when I run it on C:\\Windows\\System32\\Tasks2 , I get the System.IO.DirectoryNotFoundException exception. 但是,当我在C:\\Windows\\System32\\Tasks2上运行它时,我得到了System.IO.DirectoryNotFoundException异常。 Running the app as administrator doesn't help. 以管理员身份运行该应用程序无济于事。

What can I do in order to change permissions of a directory inside System32 directory in C#? 为了在C#中更改System32目录中目录的权限,我该怎么做?

Assuming that C:\\Windows\\System32\\Tasks2 really does exist, the most likely explanation is that you are being caught out by the file system redirector . 假设确实存在C:\\Windows\\System32\\Tasks2 ,最可能的解释是您被文件系统重定向器吸引住了。 You have a 32 bit process and the file system redirector converts system32 into SysWOW64 . 您有一个32位进程,文件系统重定向器将system32转换为SysWOW64 And so whilst you think you are looking for C:\\Windows\\System32\\Tasks2 , you are actually looking in C:\\Windows\\SysWOW64\\Tasks2 . 因此,尽管您认为自己正在寻找C:\\Windows\\System32\\Tasks2 ,但实际上您正在寻找C:\\Windows\\SysWOW64\\Tasks2

Compile your program as 64 bit. 将您的程序编译为64位。 Or use C:\\WINDOWS\\SysNative . 或使用C:\\WINDOWS\\SysNative

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

相关问题 C#类库:StreamWriter写入system32文件夹 - C# Class Library: StreamWriter writing to system32 folder 为什么在system32文件夹中找不到C#库? - Why C# library cannot be found in system32 folder? c#删除System32文件夹中的文件 - c# Deleting a file in System32 folder 如何通过C#语言删除Windows 7 dll文件(在System32文件夹中) - How Delete A Windows 7 dll File (In System32 Folder) By C# Language 如何在 C# 中获取 Windows\system32\config\systemprofile\AppData\Local\ 文件夹路径? - How to get Windows\system32\config\systemprofile\AppData\Local\ folder path in C#? 如何使用c#asp.net获取C:\\ Windows \\ System32 \\ inetsrv \\ config文件夹的访问控制 - How to get access control of C:\Windows\System32\inetsrv\config folder using c# asp.net 通过total-commander在C:\\ Windows \\ system32中打开c#文件夹 - c# open folder in C:\Windows\system32 via total-commander C#调用批处理文件无法将dll文件复制到System32文件夹 - C# calling batch file failed to copy dll files to System32 folder C#-将文件夹内容从闪存驱动器复制到System32 - C# - Copying contents of folder from flash drive to System32 在C#中访问System32 \\ config \\ SYSTEM配置单元 - Access System32\config\SYSTEM hive in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM