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