0

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"

Nițu Alexandru
  • 714
  • 1
  • 11
  • 33
  • What's the value of `path` (in the second example - which is the right way to do it)? Does it have any spaces in it? – Matt Burland Feb 25 '14 at 18:16
  • Clearly `path` contains relative path and as result file will not be opened after fixing path to exe... If it is not true - please make sure to update post with exact value of the `path`. – Alexei Levenkov Feb 25 '14 at 18:17
  • http://stackoverflow.com/questions/21564532/string-format-in-c-sharp-running-command-line/21580241#21580241 – 001 Feb 25 '14 at 18:17
  • Updated. I tried with the S/ prefix but didn't worked. With the first block of code the window is opening, the source is loaded, but I can't interact with the program. It's like a picture. Wherever I click or press a key. – Nițu Alexandru Feb 25 '14 at 18:32

2 Answers2

4

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);
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • @Alex - If the string is good, then your question contains a typo. Backslash is the escape character. You need to either escape it (with double backslashes: `"C:\\dir"`) or use a verbatim string literal (`@"C:\dir"`). If your code accounts for that already, you should correct the code in your question. – gilly3 Feb 25 '14 at 18:37
  • You are right. The program has the path correctly saved and is building the path. This is the code actually. I tried to simplify for the question: startInfo.Arguments = string.Format(@"/S /C {0}\\fp.exe ""{1}""", Settings.FpcInstallationFolder.TrimEnd('\\'), path); – Nițu Alexandru Feb 25 '14 at 18:46
1

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);
Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20