繁体   English   中英

系统在C#中通过Process模块​​运行命令时找不到指定的文件

[英]The system cannot find the file specified while running command through Process module in C#

我试图通过进程执行一个命令,但它抛出异常“系统找不到指定的文件”。 当我直接在命令提示符下运行此命令时。 它工作正常。

命令:启动 cmd.exe @cmd /k "NTttcpr.exe -r -m 1,*,192.168.1.2 -a 2 -t 120 -wu 10 -cd 10 >> NTTTCP-1T-TCP-IPV4-Rx-MTU1500 -Support-port-1-Rx-AMD-10-GBE-RJ45-ITR-1.log"

如果我在命令提示符下运行,此命令将完美执行。

我是这样写代码的:

string tool = @"NTttcpr.exe";
string command = " -r -m 1,*,192.168.1.2 -a 2 -t 120 -wu 10 -cd 10 >> NTTTCP-1T-TCP-IPV4-Rx-MTU1500-Support-port-1-Rx-AMD-10-GBE-RJ45-ITR-1.log";

private void RunCommand(string tool, string command)
    {
        try
        {
            logger.Info($"{MethodBase.GetCurrentMethod()}: {tool} {command}");
            Process pro = new Process();
            pro.StartInfo.FileName = "start cmd ";
            pro.StartInfo.Arguments = "@cmd /k " + '"' + tool  + " " + command + '"';

            pro.StartInfo.UseShellExecute = false;
            pro.StartInfo.RedirectStandardOutput = true;

            logger.Info($"{MethodBase.GetCurrentMethod()}: Executing command: {tool} {command}");
            pro.StartInfo.Verb = "runas";
            pro.Start();
            //pro.WaitForExit(MillisecondsTimeout);
            //Thread.Sleep(MillisecondsTimeout);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            logger.Error($"{MethodBase.GetCurrentMethod()}: Exception occurred while uni-directional command!!");
            logger.Error($"{MethodBase.GetCurrentMethod()}: {ex}");
        }
    }

注意:NTttcpr.exe 文件已存在于当前执行目录中。

请帮我解决这个问题。

这个应该是你没有设置工作目录,加pro.StartInfo.WorkingDirectory = "path to NTttcpr.exe"不加NTttcpr.exe,只加位置。

让我知道这个是否奏效。

Process 类不需要 cmd.exe。 尝试如下。

pro.StartInfo.FileName = "NTttcpr.exe";
pro.StartInfo.Arguments = command 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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