我在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資源管理器設置設置爲顯示隱藏文件。有任何想法嗎?
賓果!謝謝,斯科特! –
@ScottChamberlain你能否寫下你的評論作爲答案,我會把它作爲接受的答案?謝謝! –