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?