当我尝试从我的C#应用程序运行BCDEDIT时,我得到以下错误:
“‘bcdedit”不被识别为内部或外部命令、可操作的程序或批处理文件。
当我通过提升的命令行运行它时,我得到了预期的结果。
我使用了以下代码:
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;我也试过用
p.StartInfo.FileName = @"BCDEDIT.EXE";
p.StartInfo.Arguments = @"";我尝试了以下几点:
我的想法不多了,知道我为什么会犯这个错误吗?
我所需要的只是命令的输出,如果有另一种方式也能工作的话。谢谢
发布于 2012-12-24 16:06:41
有一个解释是有道理的:
bcdedit.exe文件存在于C:\Windows\System32中。C:\Windows\System32在您的系统路径上,但在x86进程中,您要受File System Redirector的约束。这意味着C:\Windows\System32实际上解析为C:\Windows\SysWOW64。bcdedit.exe中没有32位版本的C:\Windows\SysWOW64。解决方案是将您的C#程序更改为目标AnyCPU或x64。
发布于 2014-11-12 17:01:52
如果您在32it/64位Windows上都使用x86应用程序,并且需要调用bcdedit命令,下面是一种方法:
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;
}用法:
var returnCode = ExecuteBcdEdit("/set IgnoreAllFailures", out outputForInvestigation);灵感来自这个线程,来自How to start a 64-bit process from a 32-bit process和http://www.samlogic.net/articles/sysnative-folder-64-bit-windows.htm
https://stackoverflow.com/questions/14023051
复制相似问题