Question

I have a unique requirement (let's just leave it at that) to access the System32\GroupPolicy\Machine folder from C#. I can browse to the directory with full control, but when accessing from C# I get the following error:

Could not find a part of the path 'C:\windows\system32\grouppolicy\Machine'.

I can't use any other traditional methods of accessing policies, it has to be done through the file system. Here's the code I'm using just trying to browse the directory:

  string[] files;  

files = Directory.GetFiles(Environment.SystemDirectory + @"\grouppolicy\Machine");

Running Visual Studio as admin, Win 7 64 bit

EDIT

Directory.GetDirectories(Environment.SystemDirectory + @"\grouppolicy\") also returns no folders even though I see 3 subfolders in explorer.

Was it helpful?

Solution

You are almost certainly being redirected to the syswow64 directory due to 32 bit application on 64 bit OS. (just wasted 3 hours beating my head on the desk same issue). Per MSDN use SYSNATIVE instead of SYSTEM32.

Like:

    string[] files;
    if (Environment.Is64BitOperatingSystem)
    {
        files = Directory.GetFiles("C:\\WINDOWS\\SYSNATIVE\\GROUPPOLICY\\MACHINE");
    }
    else
    {
        files = Directory.GetFiles("C:\\WINDOWS\\SYSTEM32\\GROUPPOLICY\\MACHINE");
    }
    foreach (string file in files)
    {
        System.Console.WriteLine(file);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top