Search code examples
c#.netwindowswindows-sandbox

File.Exists returns false,Process.Start() can't start the file,But it actually exists


Note : Windows Sandbox is enabled on my computer.

Console.WriteLine(File.Exists(@"C:\Windows\system32\WindowsSandbox.exe"));
Console.WriteLine(new FileInfo(@"C:\Windows\system32\WindowsSandbox.exe").Exists);

Running the above code in C# Interactive(VS is not in admin mode):

True
True

However , when I run it in a console application, (both admin mode and non-admin mode).The results are always false.

False
False

I tried Process.Start(@"C:\Windows\system32\WindowsSandbox.exe"), the console application failed(admin and non-admin) but the C# interactive succeeded.

In powershell,C:\Windows\system32\WindowsSandbox.exe successfully started the Windows Sandbox. In explorer(system32 folder): image

Could anyone explain why this could happen?


Solution

  • I'm not familiar with Windows Sandbox, but it sounds like this is an architecture issue.

    On 64-bit Windows, C:\Windows\system32 is the 64-bit system directory. Explorer and PowerShell, which you say can both see C:\Windows\system32\WindowsSandbox.exe, will be 64-bit processes unless you go out of your way to run the 32-bit versions.

    In a 32-bit application, the path C:\Windows\system32 gets redirected to the 32-bit system directory, C:\Windows\SysWOW64. Since you say you don't have a C:\Windows\SysWOW64\WindowsSandbox.exe file, nor can your application see (what it thinks is) a C:\Windows\system32\WindowsSandbox.exe file, that suggests that your application is built targeting 32-bit.

    So, the problem is there is no 32-bit version of Windows Sandbox for your 32-bit application to run. When you change the path to calc.exe or notepad.exe your same code works because Windows provides both 32- and 64-bit versions of those executables. To get your application to run the 64-bit WindowsSandbox.exe, you can either...

    • Build it targeting 64-bit, or better yet...
    • Have it execute the path C:\Windows\sysnative\WindowsSandbox.exe. sysnative is a special alias that allows 32-bit applications to reference the native (64-bit) system directory without redirection.

    As for why Visual Studio is able to see C:\Windows\system32\WindowsSandbox.exe, I can't really explain that. It would depend on what version you're using, but it's my understanding that continues to be a 32-bit application, although it would make sense that some components are 64-bit. As for whether components such as the debugger would run as the same architecture as the OS or the application being built, I really don't know.