C# Open File With Associated Application passing arguments - c#

I am trying to launch the default application registered for an extension specifying an additional argument:
ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "myargument";
p.FileName = "file.ext";
Process.Start(p);
The application starts correctly opening the specified file.
The problem is that it is getting just one parameter (the name of the file), totally ignoring the additional "Arguments".
Is it possible to do what I want?
Am I doing something wrong?
Thanks in advance for any help,
Paolo

I believe this is expected. Behind the scenes, Windows is finding the default application in the registry and creating a new process and passing your file name to it. I get the same behavior if I go to a command prompt and type "filename.ext argument", that my arguments are not passed to the application.
What you probably need to do is find the default application yourself by looking in the registry. Then you can start that process with arguments, instead of trying to start by filetype association. There is an answer here on how to find the default application in the registry:
Finding the default application for opening a particular file type on Windows

what exactly is your "argument", does it have spaces, backslash, etc?
Process process = new Process();
process.StartInfo.FileName = #"C:\process.exe";
process.StartInfo.Arguments = #"-r -d something else";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
process.Start();
Is there any reason why you cant start the app, then use the extension and arguments in your arguments?

I think an easier method is using the cmd command
void LaunchAssociatedProgram(string filename) {
Process.Start( #"cmd.exe", "/C start "+ filename );
}
EDIT:
I don't know if it works with arguments, but it is what I was looking for to launch an associated program...

Related

Process.start with application & filename as variables

Have been searching, but surprisingly could not find this specific question:
With C# I want (by clicking a button in a form) to run a certain file, with an certain application.
When using "Process.start(variable)" I can only pick one of the two.
And by using "Process.startinfo.filename" (like: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.filename?view=net-5.0) this also seems to be the case.
Isn't is possible to just combine both in some "easy" way?
Thanks.
Typically you would run a file with an application using a command argument (i.e. 'notepad.exe file.txt').
If that is possible with the application(s) you are attempting to launch, then you would simply need to set the Filename property of StartInfo to the name, if in the PATH, or the full path of the application and the Arguments property to the path of the file.
var process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.Arguments = "C:\\{pathToFile}\\file.txt";
process.Start();
The above code would launch notepad opening file.txt. You can simply replace the FileName and Arguments with variables containing the paths to the application and file.

How to pass parameters to an exe?

I am using psexec on my server to run an exe file on another server. How do I pass parameters to the other exe ?
The exe that I am running on my server is psexec which in turn must run the exe named vmtoolsd.exe located on another system. How do I pass parameters to vmtoolsd.exe ? Also, where do I pass it ? Would I pass it as part of the info.Arguments ? I've tried that but it isn't working.
ProcessStartInfo info = new ProcessStartInfo(#"C:\Tools");
info.FileName = #"C:\Tools\psexec.exe";
info.Arguments = #"\\" + serverIP + #"C:\Program Files\VMware\VMwareTools\vmtoolsd.exe";
Process.Start(info);
Also, as part of info.Arguments would I have to preface the path of vmtoolsd.exe with the IP address, followed by the drive path ?
Hope the below code may help.
Code from first .exe:
Process p= new Process();
p.StartInfo.FileName = "demo.exe";
p.StartInfo.Arguments = "param1 param2";
p.Start();
p.WaitForExit();
or
Process.Start("demo.exe", "param1 param2");
Code in demo.exe:
static void Main (string [] args)
{
Console.WriteLine(args[0]);
Console.WriteLine(args[1]);
}
Right click on .exe file-->goto shortcut-->in target tab write the arguement in extreme right...
in my case it worked
You can see it in the following post (answer by #AndyMcCluggage):
How do I start a process from C#?
using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.
It provides far more control as you can see in the MSDN, but basically the arguments control is quite easy as you can see, is just to modify a property with a string.
Update: Since with the snippet code above you would be starting PsExec, based on:
PsExec
The format you have to use is:
psexec #run_file [options] command [arguments]
Where: arguments Arguments to pass (file paths must be absolute paths on the target system).
Since the process you're starting is psexec, in the process.StartInfo.Arguments you would have to put all the parameters it would need, in a sigle chain: #run_file [options] command [arguments].
Step1.Create Shortcut and then Right click on Shortcut-->click properties page then target tab write the comment line argument in extreme right... this way worked for me

Start one windows form application from another windows form application in C# [duplicate]

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

calling executable from a background process and passing a parameter to it

I have an Console application which runs as background process and there is an exe which needs to be called.This exe takes complete fill path as parameter and then encrypts that file.
I did this way :
Process.Start( "myapp.exe" );
But what i want is this :
Process.Start( "myapp.exe file1.txt" ); // File1 is parameter of that exe
But this is not working.
Looking for help & advice.
Thanks :)
You want to use the ProcessStartInfo class.
See http://msdn.microsoft.com/en-us/library/system.diagnostics.process.startinfo.aspx and http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx for an example on how to use this.
Use the Arguments property to set your arguments.
Use something like this:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "myApp.exe";
p.StartInfo.Arguments = "file1.txt";
p.Start();
Try Process.Start("myapp.exe", "file1.txt");
Process.Start("[drive]:\[directory]\myapp.exe", "file1.txt");
Substitute the actual drive and directory where indicated
Process.Start(<the nameof the process>,<the parameters>)
In your case
Process.Start("myapp.exe","file1.txt")

A question about the ProcessStartInfo class and the Argument Property?

Some sample code:
var psi = new ProcessStartInfo();
psi.FileName = "path to update.exe";
psi.Arguments = "arguments for update.exe";
psi.Verb = "runas";
var process = new Process();
process.StartInfo = psi;
process.Start();
process.WaitForExit();
Ref: 0xA3.
Programmatically in code what type of objects if possible could you pass into the '.Arguments' property? Typically you can pass an int or a string type. I want to know if you could pass in a more complicated type like a DirectoryInfo[] or a FileInfo[]? Would anyone know if this is possible? If not i'll have to come up with something else?
Why? I am trying to remove some problem code from a very large background worker and the only solution is to pass the data I require into a process that will handle the work I need doing in a completey different process. Problem this problem code always throws up on permissions - permissions the app does not have.
Serialize the data then you can "pipe" the resulting string into the other process's standard input. See example of "Process.StandardInput Property" help topic at http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx
Serialize the data, store into a file then have other process read this file, passing the path to the file in Arguments.
- Allocate the object into global memory then pass the resulting IntPtr, in Arguments, to the other process.
The arguments are exactly what you'd type (with leading and trailing quotes), if you were running the .exe from the command prompt.
Use strings.
As defined the Arguments property is of type string so you could only pass space delimited arguments to the process the same way you would when calling it at the command line.
No, this isn't possible.
You should consider making a new AppDomain in your existing process.

Categories

Resources