-1

What is wrong with this code to run a command in command prompt? I try to run this code and it does not give any error and it does not do what it is supposed to do. It works fine if I copy the command to command prompt and run it manually?

Thank you!

[TestMethod]
public void TestProcess()
{
    string command1 = @"sejda-console simplesplit --files -f C:\TestFiles\test.pdf -o C:\TestFiles\split1\ -s all";

    ProcessStartInfo processInfo;
    Process process;

    //I have the batch file sejda-console in C:\sejda-console-3.2.83\bin so I concatenated the directory of the batch file with the actual command.

    processInfo = new ProcessStartInfo("cmd.exe", @"C:\sejda-console-3.2.83\bin " + command1);  
    processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process = Process.Start(processInfo);

    process.WaitForExit();

    process.Close();
}

Alternatively, I am trying this one too which does not work either.

[TestMethod]
public void TestProcess3()
{
    string MyBatchFile = @"C:\sejda-console-3.2.83\bin\sejda-console.bat";

    string _sourcePath = @"C:\TestFiles\test.pdf";
    string _targetPath = @"C:\TestFiles\split1\";

    var process = new Process
    {
        StartInfo = {
                        Arguments = String.Format("/C simplesplit --files -f {0} -o {1} -s all", _sourcePath, _targetPath)
                    }
    };

    process.StartInfo.FileName = MyBatchFile;
    bool b = process.Start();
}
Jyina
  • 2,530
  • 9
  • 42
  • 80

2 Answers2

1
  1. You are missing /C to send arguments to cmd.exe
  2. Add backslash after \bin\
  3. Wrap your command line arguments with quotes.

So your code should look like:

[TestMethod]
public void TestProcess()
{

   string command1 = @"sejda-console simplesplit --files -f C:\TestFiles\test.pdf -o C:\TestFiles\split1\ -s all";

    ProcessStartInfo processInfo;
    Process process;

    //I have the batch file sejda-console in C:\sejda-console-3.2.83\bin so I concatenated the directory of the batch file with the actual command.

    processInfo = new ProcessStartInfo("cmd.exe", @"/C \"C:\sejda-console-3.2.83\bin\" + command1 + "\"");  
    processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process = Process.Start(processInfo);

    process.WaitForExit();

    process.Close();
}
EylM
  • 5,967
  • 2
  • 16
  • 28
  • I have been trying it. But for some reason, it does not like the syntax from this line. processInfo = new ProcessStartInfo(@"C:\Windows\System32\cmd.exe", @"/C \"C:\sejda-console-3.2.83\bin\" + command1 + "\""); – Jyina Aug 05 '19 at 18:16
  • I am trying the alternative in my updated post which does not work anyway. Thank you. – Jyina Aug 05 '19 at 18:21
1

Try this processInfo:

var batch = "sejda-console.bat";
var sourcePath = @"C:\TestFiles\test.pdf";
var targetPath = @"C:\TestFiles\split1\";

var processInfo = new ProcessStartInfo();
processInfo.WorkingDirectory = @"C:\sejda-console-3.2.83\bin";
processInfo.FileName = "cmd.exe";
processInfo.Arguments = $"/C {batch} simplesplit --files -f \"{sourcePath}\" -o \"{targetPath}\" -s all";
// todo set windows style etc

Also have a look at Executing Batch File in C# for error handling.

Suiden
  • 622
  • 4
  • 17