BCDEDIT not recognized when running via C#

前端 未结 2 1373
梦毁少年i
梦毁少年i 2021-01-05 08:15

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, oper

2条回答
  •  死守一世寂寞
    2021-01-05 08:37

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

提交回复
热议问题