簡體   English   中英

使用 c# 運行 wmic.exe 總是會產生錯誤消息“系統找不到指定的文件”。 當它在命令提示符下工作時

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM