如何修复 winmgmt 错误代码 0x8007007E?

我正在制作用于执行 winmgmt 的 C# 包装类,但命令一直失败。


我尝试将StartInfo.Verb参数设置为"runas"但没有帮助。C#应用已经提升。


在提升的命令提示符下运行命令winmgmt /verifyrepository效果很好。


public static void VerifyRepository()

{

var p = new Process();

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.FileName = @"winmgmt";

p.StartInfo.Arguments = @"/verifyrepository";

p.Start();

string output = p.StandardOutput.ReadToEnd();

p.WaitForExit();

WMIVerifyResultReceived?.Invoke(null, new WMIVerifyResultReceivedEventArgs(output));

}

当在 cmd 中运行时输出是


WMI repository is consistent


但是当使用方法运行时,VerifyRepository()我不断在同一台机器上获得此输出:


WMI repository verification failed

Error code:  0x8007007E


撒科打诨
浏览 100回答 1
1回答

慕哥9229398

导致该问题的原因是应用程序在 WOW64(64 位上的 32 位)中运行,因此 winmgmt 的路径位于 systemwow64 文件夹中,并且服务在 64 位模式下运行。通过在构建选项中取消选中“首选 32 位”来修复此问题。在不禁用首选 32 位的情况下解决此问题的另一种方法是使用 wow64apiset 在 verifyrepository 方法开始时调用Wow64DisableWow64FsRedirection,然后在最后调用Wow64RevertWow64FsRedirection函数,如下所示:[DllImport("kernel32.dll", SetLastError = true)]static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);[DllImport("kernel32.dll", SetLastError = true)]static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);public static void VerifyRepository(){var wow64Value = IntPtr.Zero;Wow64DisableWow64FsRedirection(ref wow64Value);var p = new Process();p.StartInfo.UseShellExecute = false;p.StartInfo.RedirectStandardOutput = true;p.StartInfo.FileName = @"winmgmt";p.StartInfo.Arguments = @"/verifyrepository";p.Start();string output = p.StandardOutput.ReadToEnd();p.WaitForExit();Wow64RevertWow64FsRedirection(wow64Value);WMIVerifyResultReceived?.Invoke(null, new WMIVerifyResultReceivedEventArgs(output));}请注意,如果您喜欢 32 位且未选中,则无需使用 wow64apiset 调用。
打开App,查看更多内容
随时随地看视频慕课网APP