Skip to main content
 首页 » 编程设计

c#之C :\Windows\System32\GroupPolicyUsers created with Directory. 下的文件夹创建不显示

2024年02月05日34qq号

我正在使用 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 资源管理器设置为显示隐藏文件。有什么想法吗?

请您参考如下方法:

您的应用程序是 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 页面有关重定向过程如何工作的更多信息。