简体   繁体   中英

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"

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM