Folders under C:\\Windows\\System32\\GroupPolicyUs

2019-07-29 08:23发布

问题:

I'm creating a utility in C# that will allow the manufacturing department at my company to easily Apply or Remove a predefined non-administrative Group Policy on a fresh machine. Without going into too much precise detail on this matter, the group policy restricts desktop functionality for non-administrative users on a given machine.

I have proven that if I manually copy files from one Windows 7 machine to another in the C:\Windows\System32\GroupPolicyUsers folder using the Windows Explorer and then call gpupdate /force from a command prompt, it works perfectly as I expect. Specifically, I'm copying the following folder into C:\Windows\System32\GroupPolicyUsers: S-1-5-32-545. When I try to create this directory using the "CreateDirectory" method in .NET however, the folder doesn't show up when I try to view it using Windows Explorer. I know the folder is being created because after I create the directory, I verify its existence by calling "Directory.Exists"

Here is some sample code that should illustrate the issue I'm having. Please note: you need to run this sample code "As Administrator" in order to have the proper permissions:

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();
            }
        }
    }

After running this, use the Windows Explorer to navigate to this newly created folder and you will see that it's not visible, even though I have the Windows Explorer settings set to show hidden files. Any ideas?

回答1:

Your app is 32 bit, the folder is going to C:\Windows\SysWOW64\GroupPolicyUsers either make your app AnyCPU so it will run 64 bit on 64 bit systems and 32 bit on 32 bit systems or leave it 32 bit and use the following code.

string directory;

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

the sysnative folder is a meta-folder that only exists to 32 bit apps on a 64 bit system, it redirects to the real 64 bit system32 folder.

See the MSDN page on the File System Redirector for more information on how the redirection process works.



回答2:

Thanks to a comment by user: Scott Chamberlain, I think I now understand what the issue is. My app must be a 32-bit app because indeed, the folder is getting created under C:\Windows\SysWOW64\GroupPolicyUsers.



标签: c# security