Re: Possible Bug in the .NET Framework, Please Advise
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Wed, 31 Oct 2007 19:24:34 +0100
"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:%23ELhGn9GIHA.1316@xxxxxxxxxxxxxxxxxxxxxxx
"Computer Guru" <ComputerGuru@xxxxxxxxxxxx> wrote in message news:1193844412.792072.162830@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxHello 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.
.
- Follow-Ups:
- Re: Possible Bug in the .NET Framework, Please Advise
- From: Family Tree Mike
- Re: Possible Bug in the .NET Framework, Please Advise
- References:
- Possible Bug in the .NET Framework, Please Advise
- From: Computer Guru
- Re: Possible Bug in the .NET Framework, Please Advise
- From: Willy Denoyette [MVP]
- Possible Bug in the .NET Framework, Please Advise
- Prev by Date: Re: calendar control MonthChangedEventArgs
- Next by Date: Re: C# keyword list programatically
- Previous by thread: Re: Possible Bug in the .NET Framework, Please Advise
- Next by thread: Re: Possible Bug in the .NET Framework, Please Advise
- Index(es):
Relevant Pages
|