【发布时间】:2016-05-07 07:35:13
【问题描述】:
我正在用 C# 创建一个实用程序,它允许我公司的制造部门在新机器上轻松应用或删除预定义的非管理组策略。无需详细说明此问题,组策略会限制给定计算机上非管理用户的桌面功能。
我已经证明,如果我使用 Windows 资源管理器手动将文件从一台 Windows 7 机器复制到 C:\Windows\System32\GroupPolicyUsers 文件夹中的另一台机器,然后从命令提示符调用 gpupdate /force,它就像我一样完美运行预计。具体来说,我将以下文件夹复制到 C:\Windows\System32\GroupPolicyUsers:S-1-5-32-545。但是,当我尝试使用 .NET 中的“CreateDirectory”方法创建此目录时,当我尝试使用 Windows 资源管理器查看它时,该文件夹不会显示。我知道正在创建文件夹,因为在创建目录后,我通过调用“Directory.Exists”来验证它的存在
下面是一些示例代码,可以说明我遇到的问题。请注意:您需要“以管理员身份”运行此示例代码才能获得适当的权限:
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var security = new System.Security.AccessControl.DirectorySecurity();
security.AddAccessRule(
new FileSystemAccessRule(
new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null),
FileSystemRights.ReadAndExecute,
InheritanceFlags.None | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
security.AddAccessRule(
new FileSystemAccessRule(
new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null),
FileSystemRights.ReadAndExecute,
InheritanceFlags.None | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
security.AddAccessRule(
new FileSystemAccessRule(
new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null),
FileSystemRights.ReadAndExecute,
InheritanceFlags.None | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
Directory.CreateDirectory(@"C:\Windows\System32\GroupPolicyUsers\S-1-5-32-545", security);
if (Directory.Exists(@"C:\Windows\System32\GroupPolicyUsers\S-1-5-32-545"))
{
Console.WriteLine("Directory exists.");
}
else
{
Console.WriteLine("Directory does NOT exist!");
}
Console.ReadLine();
}
}
}
运行此之后,使用 Windows 资源管理器导航到这个新创建的文件夹,您会看到它不可见,即使我已将 Windows 资源管理器设置设置为显示隐藏文件。有什么想法吗?
【问题讨论】:
-
宾果游戏!谢谢,斯科特!!!
-
@ScottChamberlain 您能否写下您的评论作为答案,我会将其设为已接受的答案?谢谢!