[英]Running wmic.exe using c# always produces the error message "The system cannot find the file specified." when it works in a command prompt
使用 c# 运行 wmic.exe 总是会产生错误消息“系统找不到指定的文件”。 当它在命令提示符下工作时。
C# 程序集是使用面向 .NET Framework 4 的“任何 CPU”构建的。
string fileName = Path.Combine(Environment.SystemDirectory, "wbem", "wmic.exe");
string arguments = @"/NAMESPACE:\\root\Microsoft\SqlServer\ComputerManagement10 PATH ServerNetworkProtocol";
Process process = new Process
{
StartInfo =
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
process.Start();
StreamReader output = process.StandardOutput;
StreamReader error = process.StandardError;
Console.WriteLine(output.ReadToEnd());
Console.WriteLine(error.ReadToEnd());
process.WaitForExit();
int exitCode = process.ExitCode;
process.Close();
Assert.AreEqual(0, exitCode);
错误信息:
Assert.AreEqual failed. Expected:<0>. Actual:<-2147024894>.
标准控制台输出:
Node - MyComputerName
ERROR:
Description = The system cannot find the file specified.
我也试过使用:
string fileName = Environment.ExpandEnvironmentVariables("%comspec%");
string arguments = string.Format(
@"/C {0} /NAMESPACE:\\root\Microsoft\SqlServer\ComputerManagement10 PATH ServerNetworkProtocol",
Path.Combine(Environment.SystemDirectory, "wbem", "wmic.exe"));
但我遇到了同样的问题。
在命令提示符下运行时,它会产生预期的输出并返回零退出代码:
c:\windows\system32\wbem\wmic.exe /NAMESPACE:\\root\Microsoft\SqlServer\ComputerManagement10 PATH ServerNetworkProtocol && echo %errorlevel%
Enabled InstanceName MultiIpConfigurationSupport ProtocolDisplayName ProtocolName
TRUE SQLEXPRESS FALSE Shared Memory Sm
FALSE SQLEXPRESS FALSE Named Pipes Np
FALSE SQLEXPRESS TRUE TCP/IP Tcp
FALSE SQLEXPRESS FALSE VIA Via
0
我也张贴在MSDN论坛在这里。
@"C:\\Windows\\System32\\wbem\\WMIC.exe"
这个路径对我@"C:\\Windows\\System32\\wbem\\WMIC.exe"
。
有关更多信息,请参阅: https : //www.computerhope.com/wmic.htm# :~: text=Wmic%20is%20an%20external%20command,%5Cwbem%5CWMIC.exe 。
问题与执行代码的测试过程有关。
要解决此问题,我必须选择:在 Visual Studio > 测试 > AnyCPU 项目的处理器架构 > x64。
选择“自动”或“x86”会导致上述错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.