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. One example was a small application that invoke Process
class’s Start
method C# (i.e. Process.Start();
) or Shell()
in VB.net.
When facing the issue, you might try to use this line of code in C#:
Process.Start("C:\\Windows\\System32\\SlideToShutdown.exe");
But it returned this error
http://media.tumblr.com/2d3f1793b40ee6ad47dd2ed5b1ad5fc7/tumblr_inline_mq2k60N7Pr1qz4rgp.jpg
When you 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:
*
Programming Guide for 64-bit Windows:*
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”.
http://media.tumblr.com/ad9d81b63176e2ff0c8f6732c7b1bbab/tumblr_inline_mq2lbjIlZi1qz4rgp.jpg
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
http://media.tumblr.com/fd0cf57cf6558881138fa8f00648dce6/tumblr_inline_mq2lltt9Om1qz4rgp.png