使用Process.Start()启动进程时出现问题 - 如何构造参数列表?

1 c# string

我有这个:

string cmd = " -i """ + finPath + """ -ar 44100 -ab 160k """ + foutPath + """";
Run Code Online (Sandbox Code Playgroud)

我需要将它从C#中传递给命令提示符Systems.Diagnostics.Process.

没有任何组合似乎有效.如果我在命令提示符下运行它,程序工作正常.如果我在VB.Net中使用相同的字符串,也运行正常

finPath有空格foutPath,它使程序不运行.

我需要finPath表达为finPath.与...相同foutPath.


更多代码(使用此处建议的行,没有运气):

string inputPath = RootPath + "videoinput\\";  

string ffmpegpath = RootPath + "ffmpeg.exe"; //ffmpeg path

string outputPath = RootPath +"videooutput\\"; 

//define new extension

string fileext = ".flv";

string newfilename = namenoextension + fileext;

string namenoextension = Path.GetFileNameWithoutExtension(savedfile);

string fileoutPath = outputPath + newfilename;

string fileinPath = "/videoinput/" + savedfile;

string cmd = " -i \"" + fileinPath + "\" -ar 44100 -ab 160k \"" + fileoutPath + "\"";


//Begin encoding process

Process proc = new Process();

proc.StartInfo.FileName = ffmpegpath;

proc.StartInfo.Arguments = cmd;

proc.StartInfo.UseShellExecute = true;

proc.StartInfo.CreateNoWindow = false;

proc.StartInfo.RedirectStandardOutput = false;

proc.Start();
Run Code Online (Sandbox Code Playgroud)

Ree*_*sey 9

这应该适合你:

string arguments = string.Format("-i \"{0}\" -ar 44100 -ab 160k \"{1}\"", finPath, foutPath);
Process.Start(thePathToExecutable, arguments);
Run Code Online (Sandbox Code Playgroud)

确保指定与命令行参数分开的可执行文件路径.


编辑以回复评论和问题编辑:

我只是在控制台中运行它,使用以下代码:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string RootPath = "C:\\";
        string savedFile = "test.avi";

        string inputPath = Path.Combine(RootPath, "videoinput");
        string ffmpegpath = Path.Combine(RootPath, "ffmpeg.exe"); //ffmpeg path
        string outputPath = Path.Combine(RootPath, "videooutput");

        //define new extension
        string fileext = ".flv";
        string namenoextension = Path.GetFileNameWithoutExtension(savedFile);
        string newfilename = namenoextension + fileext;

        string fileoutPath = Path.Combine(outputPath, newfilename);
        string fileinPath = Path.Combine(inputPath, savedFile);

        string arguments = string.Format("-i \"{0}\" -ar 44100 -ab 160k \"{1}\"", fileinPath, fileoutPath);

        Console.WriteLine(ffmpegpath);
        Console.WriteLine(arguments);
        Console.ReadKey();
    }
}
Run Code Online (Sandbox Code Playgroud)

这写道:

C:\ffmpeg.exe
-i "C:\videoinput\test.avi" -ar 44100 -ab 160k "C:\videooutput\test.flv"
Run Code Online (Sandbox Code Playgroud)

正如我所说 - 如果你这样做,它应该工作.话虽这么说,我建议阅读System.IO.Path类,并使用Path.Combine(),Path.GetFullPath()等来修复输入文件.这也可以帮助您纠正部分问题.