Avoid “File system redirection” in Visual Studio 2012

Avoid “File system redirection” in Visual Studio 2012

Windows 8.1 comes with a hidden feature called “Slide to Shutdown”, it is like the Slide to shutdown facility in Windows Phone. Every one was busy in making shortcut for the SlideToShutdown.exe on desktop and calling it via shortcut key combinations. Mine was a small application that invoke Process class’s Start method C# (i.e. Process.Start();) or Shell()in VB.net. 

I had my time and started making my idea. Then I faced an issue, I used this line of code in C#

Process.Start("C:\\Windows\\System32\\SlideToShutdown.exe");

But it returned this error

When I put a try catch, the message box pop-up said “The system can not find the file specified”. I tried with VB’s Shell("C:\\Windows\\System32\\Slidetoshutdown.exe") and C’s system("start C:\\windows\\system32\\slidetoshutdown.exe"), but each  code returned the same error.

I posted my problem in a forum and someone told me to look up “File System Redirection” and gave me this link. Yes, my problem was File system redirection in Windows.

What is File System Redirection

In the link you can see: 

The %windir%\System32 directory is reserved for 64-bit applications. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64. Access to %windir%\lastgood\system32 is redirected to %windir%\lastgood\SysWOW64. Access to %windir%\regedit.exe is redirected to %windir%\SysWOW64\regedit.exe.

That is, If I use a 64 bit Windows and call a 32 bit process, this is subjected to “File System Redirection”. It will actually look in the c:\windows\syswow64 directory for the program. The home directory for 32-bit executables. Where it doesn’t exist instead of looking in to c:\windows\system32 directory.

Most of the programs such as “notepad.exe” and “mspaint.exe” reside inside both of these system32 directory and syswow64 directory that is why,

Process.Start("C:\\windows\\system32\\mspaint.exe");

worked for me but unfortunately, “Slidetoshutdown.exe” is not available in syswow64 directory so my code did not work.

How to disable this file system redirection in Visual studio

In .NET 4.5 and Visual Studio 11 the cheese has been moved. The default for most .NET projects is again AnyCPU, but there is more than one meaning to AnyCPU now. There is an additional sub-type of AnyCPU, “Any CPU 32-bit preferred”, which is the new default (overall, there are now five options for the /platform C# compiler switch: x86, Itanium, x64, anycpu, and anycpu32bitpreferred). When using that flavor of AnyCPU, the semantics are the following:

  • If the process runs on a 32-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on a 64-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on an ARM Windows system, it runs as a 32-bit process. IL is compiled to ARM machine code.

So under Project properties -> Build tab there is a check box available, saying “Prefer 32 bit”.

The difference, then, between “Any CPU 32-bit preferred” and “x86” is only this: a .NET application compiled to x86 will fail to run on an ARM Windows system, but an “Any CPU 32-bit preferred” application will run successfully.

By default this “Prefer 32 bit” is checked, but to avoid FIle system redirection, we have to un-check it and modify our code like below.

Process.Start("slidetoshutdown.exe");

That's all, your code is going to work…

If you are using Windows 8.1, you can use my SlideToShutdown application here

Leave a Comment
  • Please add 4 and 7 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Carsten Siemens edited Revision 2. Comment: Fixed misspelling

  • Carsten Siemens edited Revision 1. Comment: Fixed misspelling

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Carsten Siemens edited Revision 1. Comment: Fixed misspelling

  • Carsten Siemens edited Revision 2. Comment: Fixed misspelling

  • Ed Price - MSFT edited Revision 5. Comment: Font style; tags

  • Congrats on winning the gold medal: blogs.technet.com/.../technet-guru-awards-august-2013.aspx

Page 1 of 1 (4 items)