Cmd进程不接受命令?

问题描述 投票:0回答:1

我正在尝试使用以下参数读取命令的输出。

ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
processStartInfo.Arguments = "nvidia-smi --query-gpu=utilization.memory --format=csv";
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;
Debug.Write("Test1 \n"); // it prints
Process process = Process.Start(processStartInfo);
using (StreamReader streamReader = process.StandardOutput)
{
    Debug.Write("Test2 \n"); // it prints
    output = streamReader.ReadToEnd();
    Debug.Write("Test3 \n"); // it doesn t print
}

String[] substrings = output.Split(delimiter2);

我应该提一下,如果我手动运行该命令,则该命令是有效的。

c# cmd
1个回答
1
投票

这里是您需要的:

ProcessStartInfo processStartInfo = new ProcessStartInfo("nvidia-smi");
processStartInfo.Arguments = "--query-gpu=utilization.memory --format=csv";
processStartInfo.UseShellExecute = false;

首先-cmd.exe不是要启动的二进制文件,因为它没有nvidia命令行参数。您实际上要启动-nvidia-smi。

如果nvidia-smi不在PATH变量中,您可能会遇到诸如找不到文件之类的异常。在这种情况下,您将必须使用FULL PATH来访问二进制文件。

更新:2020年2月25日

以下对我有用:enter image description here

还要注意,我的编译目标是64位:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.