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

  • Question

  • 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"); // c:\windows\system32\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%"); // c:\windows\system32\cmd.exe
    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

    In all cases, I am running as admin.

    Also posted on stackoverflow here:

    stackoverflow.com/questions/23089575/running-wmic-exe-using-c-sharp-always-produces-the-error-message-the-system-can

    Tuesday, April 15, 2014 9:39 PM

Answers

  • Hi RamiAbuGhazaleh,

    There must be something wrong with your command about wmic.exe. If the command was not correct, it will throw “The System cannot find the file specified” error, because cmd cannot recognize the command. Check your command first in CMD.exe.

    See the following code example about how to use Command Prompt class. http://www.codeproject.com/Tips/468480/Csharp-Command-Prompt-class.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 17, 2014 6:49 AM