Running EXE with parameters - c#

I need help in trying to execute an executable from my C# application.
Suppose the path is cPath, the EXE is HHTCtrlp.exe and the parameter that has to be passed is cParams.
How would I go about this?
The reason why the path is a variable is that there are 3 different EXE files to run and the path will change depending on which one will run, same with the parameter string.
Any help would be greatly appreciated.

To start the process with parameters, you can use following code:
string filename = Path.Combine(cPath,"HHTCtrlp.exe");
var proc = System.Diagnostics.Process.Start(filename, cParams);
To kill/exit the program again, you can use following code:
proc.CloseMainWindow();
proc.Close();

System.Diagnostics.Process.Start("PATH to exe", "Command Line Arguments");

ProcessStartInfo startInfo = new ProcessStartInfo(string.Concat(cPath, "\\", "HHTCtrlp.exe"));
startInfo.Arguments =cParams;
startInfo.UseShellExecute = false;
System.Diagnostics.Process.Start(startInfo);

Related

how can I execute a shortcut(.lnk) using Process.Start()? [duplicate]

Is there a way to run an application via shortcut from a C# application?
I am attempting to run a .lnk from my C# application. The shortcut contains a significant number of arguments that I would prefer the application not have to remember.
Attempting to run a shortcut via Process.Start() causes an exception.
Win32Exception: The specified executable is not a valid Win32 application
This is the code I am using.
ProcessStartInfo info = new ProcessStartInfo ( "example.lnk" );
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
Process whatever = Process.Start( info );
Could you post some code. Something like this should work:
Process proc = new Process();
proc.StartInfo.FileName = #"c:\myShortcut.lnk";
proc.Start();
Setting UseShellExecute = false was the problem. Once I removed that, it stopped crashing.
if your file is EXE or another file type like ".exe" or ".mkv" or ".pdf" and you want run that with shortcut link your code must like this.
i want run "Translator.exe" program.
Process.Start(#"C:\Users\alireza\Desktop\Translator.exe.lnk");
If you're using UseShellExecute = false and trying to launch a batch file make sure to add .bat to the end of the filename. You don't need .bat if UseShellExecute = true though. This made me just waste an hour of work... hoping to save someone else.

How to call C++ exe with multiple parameters from command line using C# .net

i am performing offline operations on images taking image as a input with parameters and processing it in VTK C++ exe i am unable to pass parameters to C++ exe through C# program and retrive output .
please explain me with some example
If you just mean that you have a compiled C++ program (which we'll call "foo.exe", with path stored in string "exe_folder") and you want to call that with command line parameters (stored in string "exe_params") from C#, then the following should work:
string exe_params = "target_image.jpeg HOUGH_TRANSFORM"; // Or whatever params are appropriate.
string exe_full_path = Path.Combine(exe_folder, "foo.exe");
Process proc = System.Diagnostics.Process.Start(exe_full_path, exe_params);
https://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx
Let say that your executable is called test.exe and it is in the test dir. For me, the following would be working:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C cd C:\\..test\\ && test.exe target_image.jpg yourtransformation";
process.StartInfo = startInfo;
process.Start();
If you have further problems, try setting the working directory of processStartInfo.

Run type as process in c#

I can join videos in windows at command line with
type vid1.avi vid2.avi > vidjoined.avi
I try to run this in c#:
ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
cmdStartInfo.FileName = "cmd.exe";
cmdStartInfo.Arguments = "type vid1.avi vid2.avi > vidjoined.avi";
Process cmdProcess = new Process();
cmdProcess.StartInfo = cmdStartInfo;
cmdProcess.Start();
cmdProcess.WaitForExit(120000);
What is wrong with my code?
It runs forever and I get no console output.
I'm not sure it's valid to concatenate two AVI files together, but that aside:
Try changing your arguments to:
cmdStartInfo.Arguments = "/c type vid1.avi vid2.avi > vidjoined.avi"
The "/c" will cause the command shell to execute and then exit.
Also, the working directory is going to be wherever cmd.exe is. Your relative paths to your files will likely not resolve properly. Either set cmdStartInfo.WorkingDirectory to the directory that your files are in, or use fully qualified paths in your arguments.

How to execute command-line arguments with parameters in C#?

I'm trying to compare two folders using a third party comparison tool called UltraCompare. The following line calls the program and opens two files... but this doesn't do anything except open them, plus it doesn't work properly for folders.
Process.Start("C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe",
textBoxContents1 + " " + textBoxContents2);
I'd like to use the following command line call which opens two folders, runs a comparison on them, and stores the results in output.txt: uc -d -dmf "c:\dir1" "c:\dir2" -o "c:\output.txt"
Also, I'd need to use variables for the folders instead of hardcoding the paths.
How can I work that into my C# code?
UPDATE 1:
I've modified my code according to your suggestions:
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf \"{0}\" \"{1}\" -o c:\\output2.txt",
textBoxContents1, textBoxContents2);
p.Start();
I'm wondering why the third line containing the arguments still doesn't work...
UPDATE 2:
My mistake. It is working now!! Just doesn't display the folders in UltraCompare but it is still writing and saving the output. Thanks guys!
You can use
yourProcess.StartInfo.Arguments = " .....";
Sample
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf {0} {1} -o c:\output.txt",textBoxContents1, textBoxContents2);
p.Start();
Process.Start(new ProcessStartInfo
{
FileName = #"C:\Program Files\IDM Computer Solutions\UltraCompare\uc.exe",
Arguments = String.Format("\"{0}\" \"{1}\"", textBoxContents1, textBoxContents2)
});
Make sure you use quotation marks for arguments, too!
If any of textBoxContents1 or textBoxContents2 contains spaces, you are busted!

Launch Program with Parameters

How do I write a very simple program that uses the command line to navigate to a program in the user's Program Files directory, then launches the .exe with a parameter? For example:
"C:\etc\Program Files\ProgramFolder\Program.exe C:\etc\desktop\file.spp C\etc\desktop\file.txt"
This launches a program with a certain project file and a .txt file along with it.
You can use the ProcessStartInfo.Arguments property to specify the string of arguments for your program:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\etc\Program Files\ProgramFolder\Program.exe";
startInfo.Arguments = #"C:\etc\desktop\file.spp C:\etc\desktop\file.txt";
Process.Start(startInfo);
Just create a new text file, name it "go.cmd" and put the following in there:
"C:\etc\Program Files\ProgramFolder\Program.exe C:\etc\desktop\file.spp C\etc\desktop\file.txt"
Voila, you have your program!
if you want to pass full executable path and parameters the program you need is the windows command prompt.

Categories