BCDEDIT is not recognized when running through C #

When I try to run BCDEDIT from my C # application, I get the following error:

'bcdedit' is not recognized as an internal or external command, operating program, or batch file.

when I run it using the elevated command line, I get it as expected.

I used the following code:

Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.FileName = @"CMD.EXE"; p.StartInfo.Arguments = @"/C bcdedit"; p.Start(); string output = p.StandardOutput.ReadToEnd(); String error = p.StandardError.ReadToEnd(); p.WaitForExit(); return output; 

I also tried using

 p.StartInfo.FileName = @"BCDEDIT.EXE"; p.StartInfo.Arguments = @""; 

I tried the following:

  • Checking path variables - they are fine.
  • run visual studio from an elevated command prompt.
  • full path placement.

I'm running out of ideas, any idea as to why I get this error?

all i need is the output of the command if there is another way that will work. thanks

+7
source share
2 answers

There is one explanation that makes sense:

  • The program is running on a 64-bit machine.
  • Your C # program is built as x86.
  • The bcdedit.exe file exists in C:\Windows\System32 .
  • Although C:\Windows\System32 is on your system path, in the x86 process you are exposed to a file redirector . This means that C:\Windows\System32 actually allows C:\Windows\SysWOW64 .
  • There is no 32-bit version of bcdedit.exe in C:\Windows\SysWOW64 .

The solution is to change your C # program to AnyCPU or x64 targeting.

+12
source

If you are stuck with an x86 application on both 32-bit / 64-bit Windows and you need to call the bcdedit command, here's how to do it:

 private static int ExecuteBcdEdit(string arguments, out IList<string> output) { var cmdFullFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess ? @"Sysnative\cmd.exe" : @"System32\cmd.exe"); ProcessStartInfo psi = new ProcessStartInfo(cmdFullFileName, "/c bcdedit " + arguments) { UseShellExecute = false, RedirectStandardOutput = true }; var process = new Process { StartInfo = psi }; process.Start(); StreamReader outputReader = process.StandardOutput; process.WaitForExit(); output = outputReader.ReadToEnd().Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList(); return process.ExitCode; } 

using:

 var returnCode = ExecuteBcdEdit("/set IgnoreAllFailures", out outputForInvestigation); 

The inspiration was from this topic and from How to start a 64-bit process from a 32-bit process and from http://www.samlogic.net/articles/sysnative-folder-64-bit-windows.htm

+4
source

All Articles