简体   繁体   中英

Running wmic.exe using c# always produces the error message "The system cannot find the file specified." when it works in a command prompt

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 .

@"C:\\Windows\\System32\\wbem\\WMIC.exe" this path worked for me.

For more information refer: https://www.computerhope.com/wmic.htm#:~:text=Wmic%20is%20an%20external%20command,%5Cwbem%5CWMIC.exe .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM