Search code examples
c#markdownpandocepub

How changing directories in Pandoc using C#?


How to set a directory and execute a file conversion in Pandoc using C#.

        string processName = "pandoc.exe";          
        string arguments = @"cd C/Users/a/Desktop/ConvertFileApp/ConvertFileApp/bin/Debug/marcdovd "
                          + "chapter1.markdown "
                          + "chapter2.markdown "
                          + "chapter3.markdown "
                          + "title.txt "
                          + "-o progit.epub";

        var psi = new ProcessStartInfo
        {
            FileName = processName,
            Arguments = arguments,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardInput = true
        };

        var process = new Process { StartInfo = psi };
        process.Start();

this code does not work.


Solution

  • You're calling the executable with the cd command as an argument. That's the equivalent of running the following on the command line:

    pandoc.exe cd C/Users/a/Desktop/ConvertFileApp/ConvertFileApp/bin/Debug/marcdovd chapter1.markdown chapter2.markdown chapter3.markdown title.txt -o progit.epub
    

    While I'm not familiar with Pandoc, I imagine you actually want to do something like this:

    cd C:/Users/a/Desktop/ConvertFileApp/ConvertFileApp/bin/Debug/marcdovd
    pandoc.exe chapter1.markdown chapter2.markdown chapter3.markdown title.txt -o progit.epub
    

    To do this, remove the cd command from your arguments and set the ProcessStartInfo.WorkingDirectory property like so:

        string processName = "pandoc.exe";          
        string arguments = "chapter1.markdown "
                          + "chapter2.markdown "
                          + "chapter3.markdown "
                          + "title.txt "
                          + "-o progit.epub";
    
        var psi = new ProcessStartInfo
        {
            FileName = processName,
            Arguments = arguments,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardInput = true,
            WorkingDirectory = @"C:/Users/a/Desktop/ConvertFileApp/ConvertFileApp/bin/Debug/marcdovd"
        };
    
        var process = new Process { StartInfo = psi };
        process.Start();