-1

I am creating a program that has to work with Windows system programs like a C:\windows\System32\bcdedit.exe. If I try to reach for example mspaint it works good.

IO.File.Exists(@"C:\windows\System32\mspaint.exe") // return true

but

IO.File.Exists(@"C:\windows\System32\bcdedit.exe") // return false

This returns false but the file really exists. I can see it in windows explorer and I can start it from the command line. Only for my c# application is this file unreachable. When I want to start it I get the error Win32Exception with the message:

The system cannot find the file specified

And when I "ask" if the file exists (by code above) it returns false.

Why?

ProgrammingDude
  • 597
  • 1
  • 5
  • 25
Misaz
  • 3,694
  • 2
  • 26
  • 42
  • 2
    what if you do this instead `IO.File.Exists(@"C:\windows\System32\bcdedit.exe");` – MethodMan Dec 23 '15 at 19:52
  • ooh, when I was rewriting my code I forget to @. But with @ is the result the same. – Misaz Dec 23 '15 at 19:55
  • I run the code using the following and it works on my machine `if (File.Exists(@"C:\windows\System32\bcdedit.exe"))` also you need an If statement is what I am guessing.. – MethodMan Dec 23 '15 at 20:00
  • 2
    Change your build configuration to compile to AnyCPU and that should do the trick. An x86 app that tries to access the Windows folder will actually access the C:\Windows\SysWOW64 folder on a 64-bit system. You need to be a 64-bit app to get to 64-bit folders. from https://social.msdn.microsoft.com/forums/vstudio/en-US/050df01f-eb61-49c1-9109-3c1954dcf794/fileexists-and-system32-issue – dbugger Dec 23 '15 at 20:02
  • AnyCPU is default (and it does not works) but I changed it to x64 and it works. Thank you. – Misaz Dec 23 '15 at 20:06
  • 1
    do you use any 3rd party assemblies that are 64 bit and not 32 bit perhaps bcedit.exe will only run in 64 bit because that's the Operating system that you have on your local machine.. also like I've stated I tested your exact code and I have 64 bit OS and set my build by default to `AnyCPU` and it worked. so perhaps you have code else where that you are not showing that relies on a x64 setting – MethodMan Dec 23 '15 at 20:08
  • I don't use any 3rd party assemblies. Only assemblies what are default packed in Windows Forms Application template. – Misaz Dec 23 '15 at 20:11
  • only one thing what i did, I add app.manifest and set that app require Administration permission. Nothing else. – Misaz Dec 23 '15 at 20:13
  • Well @Misaz I tested this just now in a console app and I can't get it to fail.. try setting both platforms to AnyCPU you must be doing something else wrong .. what does the App.Config look like.. do you have some old references in there..? – MethodMan Dec 23 '15 at 20:13

1 Answers1

0

Try the following, this should return true.

IO.File.Exists(@"C:\windows\System32\bcdedit.exe");

Here is an example to test:

string curFile = @"C:\windows\System32\bcdedit.exe";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");

Reference:https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx

If that does not work, then most likely issue is x64 or x86. Therefore you should configure your build to AnyCPU and test it again.

enter image description here

More information could be found here.

Community
  • 1
  • 1
casillas
  • 16,351
  • 19
  • 115
  • 215