Nițu Alexandru
Nițu Alexandru

Reputation: 714

Start process with parameter

You can use this command to start FreePascal from Command Prompt with a source to load: C:\FPC\2.6.2\bin\i386-win32\fp.exe 2.pas, where the first argument is the path to the FreePascal executable and 2.pas is a source. Now, I want to open sources like this from C#. I already tried this but didn't work:

Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false; //required to redirect
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = string.Format("/C C:\\FPC\\2.6.2\\bin\\i386-win32\\fp.exe \"{0}\"", sourcePath);

process.StartInfo = startInfo;
process.Start();

And

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\FPC\\2.6.2\\bin\\i386-win32\\fp.exe";
startInfo.Arguments = path;
Process.Start(startInfo);

Do you have any suggestion? Thank you!

UPDATE Example of path value: "C:\FPC\2.6.2\bin\i386-win32\3.pas"

Upvotes: 0

Views: 1472

Answers (2)

gilly3
gilly3

Reputation: 91467

Try adding an @ to your string, making it a verbatim string literal, so the backslashes are left alone:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\FPC\2.6.2\bin\i386-win32\fp.exe";
startInfo.Arguments = path;
Process.Start(startInfo);

Or:

startInfo.FileName = "cmd.exe";
startInfo.Arguments = string.Format("/C {0} \"{1}\"",
    @"C:\FPC\2.6.2\bin\i386-win32\fp.exe", sourcePath);

Upvotes: 4

Wasif Hossain
Wasif Hossain

Reputation: 3940

You have to prepend the path strings with @. Please follow the code below:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\FPC\2.6.2\bin\i386-win32\fp.exe";
startInfo.Arguments = @"2.pas";
Process.Start(startInfo);

Upvotes: 1

Related Questions