Search code examples
c#.netwpfpixelsense

Use relative Path in Microsoft Surface application?


I convert my wave file into a mp3 file by the following code:

internal bool convertToMp3()
        {
            string lameEXE = @"C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\lame\lame.exe";
            string lameArgs = "-V2";

            string wavFile = fileName;
            string mp3File = fileName.Replace("wav", "mp3");

            Process process = new Process();
            process.StartInfo = new ProcessStartInfo();
            process.StartInfo.FileName = lameEXE;
            process.StartInfo.Arguments = string.Format("{0} {1} {2}", lameArgs, wavFile, mp3File);

            process.Start();
            process.WaitForExit();

            int exitCode = process.ExitCode;
            if (exitCode == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

This works, but now I'd like to not use the absolut path to the lame.exe but a relative path. I included a lame.exe in the folder /lame/ on the root of the project. How can I reference it?


Solution

  • If you have rights to distribute the file with your application, then one way of doing it would be to include the .exe file as an item in your C# project, with

    Build Action = None
    Copy to Output Directory = Copy if newer
    

    You can then just use

    string lameEXE = @"lame.exe"