1

I have created a 32-bit application in C#. Because it's 32-bit, it cannot access 64-bit locations such as C:\Windows\System32. To access 64-bit locations, I understand that I need to instead use C:\Windows\Sysnative.

I want to check if a file exists in System32. I placed a file there and can view it in my file explorer, so I know that it exists.

However, when I use the following code, the File.Exists method always returns false. Sysnative doesn't seem to be redirecting at all. How can I use File.Exists with both %windir% and Sysnative to find a file in System32 from a 32-bit app?

string fileName = "someFile.dll";
string path = Environment.ExpandEnvironmentVariables(@"%windir%\Sysnative\" + fileName);
//path should be 'C:\Windows\System32\'+fileName;

bool fileExists = File.Exists(path); //Always returns false
user2465164
  • 917
  • 4
  • 15
  • 29
  • This should work provided that you also include the file extension on the file name. – jegtugado Nov 08 '17 at 00:45
  • @JohnEphraimTugado Yup, the file extension is included. Edited original post to be more clear. I'm under the impression that it's some weird combo of using %windir%, Sysnative, Environment.ExpandEnvironmentVariable, and File.Exists together. But it doesn't seem like a very special situation--if it's a problem, surely someone has documented it somewhere? – user2465164 Nov 08 '17 at 00:51
  • 1
    Are you sure your application is compiled as 32-bit? Not AnyCpu? Your code should work. – Blorgbeard Nov 08 '17 at 00:57

1 Answers1

4

I did a quick test and it turns out that if you are using x64 then fileExists returns False while it returns True for x86 and Any CPU. As to the reason why, I don't know but since you plan on using this on a 32-bit application then simply use x86. Make sure to choose x86 so that Any CPU won't mistakenly pick x64.

eryksun:

The reason is simply that "SysNative" isn't a real directory. It gets redirected to the real "System32" directory by the WOW64 emulation system. Only 32-bit applications running on 64-bit Windows via WOW64 can use this virtual directory. A 32-bit application running on 32-bit Windows cannot use "SysNative", so "System32" has to be tried even in a 32-bit app.

CODE

class Program
{
    static void Main(string[] args)
    {
        string fileName = "aeevts.dll";
        string path = Environment.ExpandEnvironmentVariables(@"%windir%\Sysnative\" + fileName);
        //path should be 'C:\Windows\System32\'+fileName;

        Console.WriteLine(path);

        bool fileExists = File.Exists(path); //Always returns false

        Console.WriteLine(fileExists);

        Console.Read();
    }
}

Properties

enter image description here

OP

Running on Any CPU, only if you have 'prefer 32-bit' checked on the Platform Target (see above screenshot) will Sysnative work and the File.Exists returns true

x86/Any CPU Output

enter image description here

x64 Output

enter image description here

jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • 3
    The reason is simply that "SysNative" isn't a real directory. It gets redirected to the real "System32" directory by the WOW64 emulation system. Only 32-bit applications running on 64-bit Windows via WOW64 can use this virtual directory. A 32-bit application running on 32-bit Windows cannot use "SysNative", so "System32" has to be tried even in a 32-bit app. – Eryk Sun Nov 08 '17 at 08:28
  • I see. So if OP plans on running this on a 32-bit machine then he cannot use "SysNative". He should use "System32" instead since 32-bit machines don't have the WOW64 emulation system. Makes perfect sense to me. – jegtugado Nov 08 '17 at 08:49
  • John, between you and eryksun, this seems to be the answer. However, I'd also like to point out that running on Any CPU, only if you have 'prefer 32-bit' checked on the Platform Target (see above screenshot) will Sysnative work and the File.Exists returns true. I was using Any CPU but had 'prefer-32' turned off and was returning false, which changed to true when I turned it on. I think you should edit your answer to include this and eryksun's explanation. (: Thanks! – user2465164 Nov 08 '17 at 16:46