【问题标题】:Folders under C:\Windows\System32\GroupPolicyUsers created with Directory.Create not showing up使用 Directory.Create 创建的 C:\Windows\System32\GroupPolicyUsers 下的文件夹未显示
【发布时间】: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 您能否写下您的评论作为答案,我会将其设为已接受的答案?谢谢!

标签: c# security


【解决方案1】:

您的应用程序是 32 位的,文件夹将转到 C:\Windows\SysWOW64\GroupPolicyUsers 或者将您的应用程序设为 AnyCPU,这样它将在 64 位系统上运行 64 位,在 32 位系统上运行 32 位,或者将其保留为 32 位并使用以下内容代码。

string directory;

if(Environment.Is64BitOperatingSystem)
{
    directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows),"Sysnative");
}
else
{
    directory = Environment.GetFolderPath(Environment.SpecialFolder.System);
}

sysnative 文件夹是一个元文件夹,仅存在于 64 位系统上的 32 位应用程序中,它重定向到真正的 64 位 system32 文件夹。

有关重定向过程如何工作的详细信息,请参阅 File System Redirector 上的 MSDN 页面。

【讨论】:

  • 斯科特张伯伦,谢谢你的回答!这真的帮助了我很多。我希望我能早点发布这个问题,而不是为此苦苦挣扎几个小时!
【解决方案2】:

感谢用户的评论:Scott Chamberlain,我想我现在明白问题所在了。我的应用程序必须是 32 位应用程序,因为确实,该文件夹是在 C:\Windows\SysWOW64\GroupPolicyUsers 下创建的。

【讨论】:

  • @ScottChamberlain:我没有注意到时间戳。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 2013-01-15
相关资源
最近更新 更多