process.startInfo.Arguments: 无法打开文件

process.startInfo.Arguments: cannot open file

我正在尝试使用以下代码

在 windows 上启动 libsvm 的 svm_scale
Process a = new Process();

a.StartInfo.FileName = ("C:\Users\Projects\Documents\libsvm-master\windows\svm-scale");

a.StartInfo.Arguments = ("-l -1 -u 1 -s range C:\Users\Projects\Documents\MyAttempts\Attempt_3\Final_Final.txt>C:\Users\Projects\Documents\MyAttempts\Attempt_3\Scale.scale");

a.Start();

但不幸的是,当 运行 这段代码时,它显示 "cannot open file C:\Users\Projects\Documents\MyAttempts\Attempt_3\Final_Final.txt>C:\Users\Projects\Documents\MyAttempts\Attempt_3\Scale.scale"

但是当我在命令提示符下键入相同的命令时,它工作正常并在指定的目标中创建 Scale.scale 文件。 请帮我解决这个问题......谢谢..

问题是参数列表中的标准输出重定向部分(“>”运算符之后的部分)被当作另一个参数对待。

这是因为输出重定向是 windows shell 为您执行的操作,而您正在使用 .net 启动 svm-scale 程序而不是 shell。

如果您希望捕获您启动的进程的标准输出,您应该使用 ProcessStartInfo.RedirectStandardOutput property combined with stream writing to a file. This has been answered in the past on stack overflow - for example here

finnaly I found a way to solve my error.


var startInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                };

                var process = new Process { StartInfo = startInfo };

                process.Start();
                process.StandardInput.WriteLine(@"C:\Users\Batu\Documents\libsvm-master\windows\svm-scale -l -1 -u 1 -s range C:\Users\Batu\Documents\MyAttempts\Attempt_3\Final_Final.txt>C:\Users\Batu\Documents\MyAttempts\Attempt_3\Scale.scale");
                //process.StandardInput.WriteLine(@"dir>c:\results2.txt");
                process.StandardInput.WriteLine("exit");

                process.WaitForExit();