Re: Possible Bug in the .NET Framework, Please Advise

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:%23ELhGn9GIHA.1316@xxxxxxxxxxxxxxxxxxxxxxx
"Computer Guru" <ComputerGuru@xxxxxxxxxxxx> wrote in message news:1193844412.792072.162830@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello All,

I'm at my wit's end on this one, and would appreciate any help or
insight that could be given.

Environment: Windows Vista x64 ONLY

Code:

//START BUG
string bcdedit = Environment.SystemDirectory + @"\bcdedit.exe";
Console.WriteLine(bcdedit);
if (File.Exists(bcdedit))
Console.WriteLine("bcdedit.exe found at location" + bcdedit);
else
Console.WriteLine("bcdedit.exe was not found at " + bcdedit);
//END BUG


Output:

C:\Windows\System32\bcdedit.exe
bcdedit.exe was not found at location C:\Windows\System32\bcdedit.exe



----------------------


Problem is.... I *KNOW* the file exists in C:\Windows\System32\ and
that its name is bcdedit.exe
I can see the file, I can access it, I can copy it, and I can just
about anything else that I want with it.


To me, finding files should be like duck typing:
If you can see the duck and you can copy the duck, then the duck
definitely exists.


SO

The duck exists. It's there for sure. Yet the .NET 2.0 (or is it 3.0
in Vista? I forget) Framework can't see it :/

Perhaps someone can point me in the right direction here?

This code works just fine on Vista x86.

Is this really a .NET bug?

NOTE: Attempting to access the file (without checking that it exists)
crashes the program - as is expected from the above behavior.

Desperately awaiting a sane explanation to all this.
-CG





This works if your code runs as 64bit!, so you need to make sure you have set the platform to "X64" or "AnyCpu".

Willy.



I suppose I should expand a bit on this, the reason that your code fails is a result of what is called " Syswow64 redirection".
"%Systemroot%\System32" is reserved for 64 bit applications, the File System redirects all accesses from 32 bit applications to "%Systemroot%\SysWOW64" and as this directory does not include bcdedit.exe, your code fails to find the executable file.

One option to solve this is to compile the program as 64bit or MSIL, another is to bypass the FS redirection by using "%Systemroot%Sysnative" whenever you need to access the native System32 directory.
Your sample code will work as expected when you change this line:
string bcdedit = Environment.SystemDirectory + @"\bcdedit.exe";

into:
string bcdedit = Environment.ExpandEnvironmentVariables(@"%systemroot%\Sysnative") + @"\bcdedit.exe";

Willy.


.



Relevant Pages

  • Possible Bug in the .NET Framework, Please Advise
    ... Environment: Windows Vista x64 ONLY ... //START BUG ... If you can see the duck and you can copy the duck, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Possible Bug in the .NET Framework, Please Advise
    ... Environment: Windows Vista x64 ONLY ... //START BUG ... If you can see the duck and you can copy the duck, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Possible Bug in the .NET Framework, Please Advise
    ... There is similar redirection with registry keys under HKLM. ... Environment: Windows Vista x64 ONLY ... //START BUG ... If you can see the duck and you can copy the duck, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: WS_CAPTION bug (only on Vista?)
    ... void setfs(HWND parent, HWND child, int fullscreen) ... Since the bug appears in Vista and not in XP or Win7 I am surprised to ... I have a couple Vista systems and thought I'd give it a shot, ... specification of what windows are being passed in. ...
    (microsoft.public.vc.mfc)
  • Re: Vista SP1 does not stop INTERNAL_POWER_ERROR Blue Screen
    ... The article says that this is fixed in Vista SP1. ... BTW, that bug was eliminated months ago, please try to keep up. ... I have installed Ubuntu on dozens and dozens of laptops and desktops ...
    (microsoft.public.windows.vista.general)