2016-05-06 74 views
1

我在C#中創建一個實用程序,這將允許我公司的製造部門輕鬆應用或刪除預定義的非管理組策略在新鮮的機器上。在這個問題上,不用過多精確的細節,組策略就會限制給定機器上的非管理用戶的桌面功能。C: Windows System32 GroupPolicyUsers下的文件夾與Directory.Create創建不顯示

我已經證明,如果我使用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資源管理器設置設置爲顯示隱藏文件。有任何想法嗎?

+0

賓果!謝謝,斯科特! –

+0

@ScottChamberlain你能否寫下你的評論作爲答案,我會把它作爲接受的答案?謝謝! –

回答

3

您的應用程序是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文件夾是一種元文件夾的存在只是爲了32個應用程序在64位系統上,它重定向到真正的64位system32目錄。

有關重定向過程如何工作的更多信息,請參見File System Redirector上的MSDN頁面。

+0

Scott Chamberlain,謝謝你的回答!這真的幫了我很多。我希望我早些時候發佈這個問題,而不是在這個小時內掙扎! –

0

感謝來自用戶的評論:Scott Chamberlain,我想我現在明白了問題所在。我的應用程序必須是32位應用程序,因爲該文件夾正在C:\ Windows \ SysWOW64 \ GroupPolicyUsers下創建。

+0

@ScottChamberlain:我沒有注意到時間戳。謝謝。 –

相關問題