C# 启动控制台程序(跛脚)并执行它



我写了一个c#程序。现在我想用 lame 将.avi文件转换为 .mp3 个文件。我已经安装了命令行应用程序。手动它工作正常。现在我想自动化该过程:

C# 应用程序 => 启动控制台并使用参数运行"lame.exe"。

我想转换多个文件。我该怎么做?

谢谢

我不知道 lame 是如何工作的,但您不能获得要转换的所有文件的列表,使用 foreach 循环遍历列表并为每个文件运行"lame.exe"吗?

如果您可以使用CMD手动调用Lame,则应该可以对此执行相同的操作:

public void ConvertFileWithLame(string pathToLame, string fileToConvert){
     // Use ProcessStartInfo class.
     ProcessStartInfo startInfo = new ProcessStartInfo(pathToLame, fileToConvert);
     try{
          // Start the process with the info we specified.
          // Call WaitForExit and then the using-statement will close.
          using (Process exeProcess = Process.Start(startInfo)){
               exeProcess.WaitForExit();
          }
     }
     catch{
          // Log error.
     }
}

P.D:请记住,您的命令必须在 PATH 中,或者在"/path/commandName.exe"上指示路径

您所要做的就是为 Directory.GetFiles 实现一个foreach循环,然后在 foreach 循环中使用Process.Start来运行您的命令。请参阅下面的示例代码:

foreach(var t in Directory.GetFiles("path"))
{
  System.Diagnostics.Process.Start("cmd.exe", $"lame command here with interpolated values (t will point to full path of file)");
}
你可以

尝试做的是看看lame.exe是否接受文件名的多个参数,所以与其遍历文件名,不如像下面这样添加它们

Process.Start("lame.exe", "file1 file2 file3 etc");

最新更新