Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BCDEDIT not recognized when running via C#

Tags:

c#

windows

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, operable program or batch file.

when i run it via elevated command line i get as expected.

i have 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 have also tried using

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

i have tried the following:

  1. Checking path variables - they are fine.
  2. running visual studio from elevated command prompt.
  3. placing full path.

i am running out of ideas, any idea as to why i am getting this error ?

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

like image 706
james Avatar asked Dec 24 '12 15:12

james


People also ask

How do I open BCDEdit in Windows 11?

Use the command line help to view optionsC:\> BCDEdit /? BCDEDIT - Boot Configuration Data Store Editor The Bcdedit.exe command-line tool modifies the boot configuration data store.

How do I restore BCDEdit?

To Restore Boot Configuration BCD Store in Windows 10 Open an elevated command prompt, or a command prompt at boot. Type the following command, and press Enter. bcdedit /import "<full path to your file>. bcd" .


2 Answers

There is one explanation that makes sense:

  1. You are executing the program on a 64 bit machine.
  2. Your C# program is built as x86.
  3. The bcdedit.exe file exists in C:\Windows\System32.
  4. Although C:\Windows\System32 is on your system path, in an x86 process you are subject to the File System Redirector. Which means that C:\Windows\System32 actually resolves to C:\Windows\SysWOW64.
  5. There is no 32 bit version of bcdedit.exe in C:\Windows\SysWOW64.

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

like image 146
David Heffernan Avatar answered Oct 17 '22 19:10

David Heffernan


If you are stuck with x86 application on both 32it/64bit Windows and You need to call bcdedit command, here is a way how to do that:

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;
}

usage:

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

Inspiration was from this thread 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

like image 41
user2126375 Avatar answered Oct 17 '22 17:10

user2126375