Search code examples
c#sox

Use of Sox.exe in c# project


I made myself a proof of concept to test this tool to manage audio files.My purpose is to change sample rate. My first example works fine!

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();
        }
    }
}

But when I want to add this tool in my bin folder but I get the exception: The directory name is invalid

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();
        }
    }

Maybe its very obvious but I dont know what I'm doing wrong


Solution

  • Your working directory is wrongly set. Use AppDomain.CurrentDomain.BaseDirectory instead. That will make the Process to start at bin folder. Then, replace your file and your arguments to work relative to the working directory (thus remove the bin part of your path).

    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();
            }
        }