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# assembly is built with "Any CPU" targeting .NET Framework 4.
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);
Error Message:
Assert.AreEqual failed. Expected:<0>. Actual:<-2147024894>.
Standard Console Output:
Node - MyComputerName
ERROR:
Description = The system cannot find the file specified.
I have also tried using:
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"));
but I experience the same issue.
When run on the command prompt, it produces the expected output and returns an exit code of zero:
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
I've also posted on the MSDN forums here.
The problem was related to the test process executing the code.
To resolve this issue I had to select: In Visual Studio > Test > Processor Architecture for AnyCPU Projects > x64.
Selecting "Auto" or "x86" results in the error that was mentioned.