Sox.exe在c#项目中的使用

Sox.exe在c#项目中的使用,c#,sox,C#,Sox,我自己做了一个概念验证来测试这个工具来管理音频文件。我的目的是改变采样率。我的第一个例子很好用 public class Test { public void SoxMethod() { var startInfo = new ProcessStartInfo(); startInfo.FileName = "C:\\Program Files (x86)\\sox-14-4-2\\sox.exe"; startInfo.Argum

我自己做了一个概念验证来测试这个工具来管理音频文件。我的目的是改变采样率。我的第一个例子很好用

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
文件夹开始。然后,将文件和参数替换为相对于工作目录工作的文件和参数(从而删除路径的
bin
部分)


尝试将工作目录设置为可执行文件的文件夹,如下所示:
startInfo.WorkingDirectory=AppDomain.CurrentDomain.BaseDirectory
首先,您可以使用Console.WriteLine(directory.GetCurrentDirectory())检查当前目录是什么然后您可以使用Path.Combine和正确的路径AppDomain.CurrentDomain.BaseDirectory这是返回Binf中的debuf文件夹,具体取决于您的输出生成配置。调试文件夹外是否有sox-14-4-2?
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();
        }
    }