我自己做了一个概念验证来测试这个管理音频文件的工具。我的目的是改变采样率。我的第一个示例运行良好!
public class Test
{
public void SoxMethod()
{
var startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files (x86)\\sox-14-4-2\\sox.exe";
startInfo.Arguments = "\"C:\\Program Files (x86)\\sox-14-4-2\\input.wav\" -r 16000 output.wav";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.WorkingDirectory= "C:\\Program Files (x86)\\sox-14-4-2";
using (Process soxProc = Process.Start(startInfo))
{
soxProc.WaitForExit();
}
}
}
但是当我想在我的 bin 文件夹中添加这个工具时却出现异常:目录名称无效
public void SoxMethod()
{
var startInfo = new ProcessStartInfo();
startInfo.FileName = "bin/sox-14-4-2/sox.exe";
startInfo.Arguments = "bin/sox-14-4-2/input.wav -r 16000 output.wav";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.WorkingDirectory= "bin/sox-14-4-2";
using (Process soxProc = Process.Start(startInfo))
{
soxProc.WaitForExit();
}
}
也许它很明显,但我不知道我做错了什么
最佳答案
您的工作目录设置错误。请改用 AppDomain.CurrentDomain.BaseDirectory
。这将使进程从 bin
文件夹开始。然后,将您的文件和参数替换为 relative 工作目录(因此删除路径的 bin
部分)。
public void SoxMethod()
{
var startInfo = new ProcessStartInfo();
startInfo.FileName = "sox-14-4-2/sox.exe";
startInfo.Arguments = "sox-14-4-2/input.wav -r 16000 output.wav";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
using (Process soxProc = Process.Start(startInfo))
{
soxProc.WaitForExit();
}
}
关于c# - c#项目中Sox.exe的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34021582/