Launch a file with command line arguments without knowing location of exe? - c#

Here's the situation: I am trying to launch an application, but the location of the .exe isn't known to me. Now, if the file extension is registered (in Windows), I can do something like:
Process.Start("Sample.xls");
However, I need to pass some command line arguments as well. I couldn't get this to work
Process p = new Process();
p.StartInfo.FileName = "Sample.xls";
p.StartInfo.Arguments = "/r"; // open in read-only mode
p.Start();
Any suggestions on a mechanism to solve this?
Edit # aku
My StackOverflow search skills are weak; I did not find that post. Though I generally dislike peering into the registry, that's a great solution. Thanks!

Using my code from this answer you can get command associated with xls extension. Then you can pass this command to Process.Start method.

If you query the registry, you can retrieve the data about the registered file type and then call the app directly passing the command line arguments. See Programmatically Checking and Setting File Types for an example of retrieving shell information for a file type.

Related

Change the working directory of a running process with C#

I do not know if this is even possible without breaking/crashing the process but is there a way to change the working directory of a System.Diagnostics.Process like you would when executing the cd (change directory) command from the cmd.exe command line interface?
You may set the working directory of the process with
myProcess.StartInfo.WorkingDirectory = "dir".
Documentation here.
As per MSDN, there is only one function which can change current folder, SetCurrentDirectory and it has single string parameter, so the change is for current process only.

Open a PDF with arguments (Page/Named Destination) WITHOUT referencing a specific program

thanks for taking the time to read my quandary.
I need to open a PDF (C# 4.0, winforms). Now normally this is pretty easy if you aren't passing in arguments (Process.Start to the PDF), but I need to be able to pass in arguments (go to a specific page or named destination right now, maybe more later).
Now I know if you call AcroRd32 you can pass in the arguments needed to do this, but it is possible that the user won't have Acrobat Reader installed on their machine, so I don't really want to call that exe necessarily. Anyone know of a way to pull this off?
Am I missing an obvious way to do this using something else?
Thanks for your help!
PS - replies can be in C# or VB, don't care
To pass the arguments in Process.Start(), you need to call AcroRd32.exe or Acrobat.exe, like:
Process myProcess = new Process();
myProcess.StartInfo.FileName = "acroRd32.exe"; //not the full application path
myProcess.StartInfo.Arguments = "/A \"page=2=OpenActions\" /n C:\\sample.pdf";
myProcess.Start();
I suggest putting this is a Try/Catch block, and if it fails then try "Acrobat.exe" as the FileName in another, nested Try/Catch block, and if that fails, default to:
Process.Start("C:\\sample.pdf");
which doesn't allow you to open to the specific page, but at least the PDF will open if the first two approaches have failed.

Launching a file using Process.Start works, but adding conditions does not

So I am trying to launch a printer script using cscript from C#, and cscript launches a visual basic file. So sort of a daisy chain (and I want to keep this daisy chain intact for certain reasons).
Here's the code:
Process.Start("c:/windows/system32/cscript.exe c:/windows/System32/Printing_Admin_Scripts/en-US/prnport.vbs");
Now, when I launch ONLY cscript, no problems.
However when I add the condition of prnport.vbs to the cscript launch, I get this error in Visual Studio:
"The system cannot find the file specified"
But I can confirm the file path is correct - prnport.vbs DOES exist in /en-US.
So what am I doing wrong here? Can you not pass arguments (and in this case, the file path is being passed as an argument to cscript.exe) when using Process.Start?
New to C# and confused about the proper way to do this.
You have to specify the arguments separately from the file to run. Try the Process.Start(string, string) overload:
Process.Start("c:/windows/system32/cscript.exe",
"c:/windows/System32/Printing_Admin_Scripts/en-US/prnport.vbs");
That's an Argument, you'll need to use another overload of Process.Start
Have a look at the method's documentation.
Process.Start (String, String) will do, others are possible and offer more flexibility, if you should need that, too.
The Process.Start expects the file name as the first parameter. The arguments are given in separate argument.

Get list of all possible command line arguments for an exe

Is there anyway for me to get the list of available command line arguments for an exe? I need to create a Powershell script that start the Relius Agent Manager program, but this program requires database login and password info to load.
I can obtain the login/pass info, but I need to know the names of the argument I'll need to pass them through.
It doesn't matter if I use Powershell or C# to retrieve the list of arguments, I just need to know their darn names.
I read up on the Process class in C# but I don't see anything that will actually list the available arguments, only how to list what was passed when a process was started.
There is no way to query the supported command line arguments. Try running the program from a command prompt with no arguments, with an invalid argument, with /h, /?, -h, --help and similar arguments to see if one of these prompts the program to output a list of allowed arguments.
Thanks for all of your help. I was able to solve it by having someone start it manually and then log in, and then I checked this list of all running processes with this command:
"WMIC /OUTPUT:C:\ProcessList.txt PROCESS get Caption,Commandline,Processid"
I was then able to find exactly what was passed when it was run from the file that command generated and execute that command in PowerShell using something like:
$exp = "&'C:\path\to\the\exe.exe' /param1 /param2 /param3"
invoke-expression $exp
search for universal silent switch finder. it is actually possible to do programmatically but requires deep diving into binary executable. if interested check http://code.google.com/p/pefile/wiki/PEiDSignatures

What steps shall I take before building a command line application using C#

I want to develop a command line tool. I didn't build one before, but I used a few, (like mercurial). What steps do I need to take to know how to do that? So what is the problem:
Regular console application need to be invoked from the command line only from it's directory. Like: C:\Projects\CommanLineProject\MyProjectConsole.exe. I want to use it from the command line from any directory, like mercurial.
I don't sure how to parse and how to use the command-line arguments. There are programs that taking this arguments: c:\>MyProject "c:\point to this path" /xml:"here is the another path" /r.
A command line tool is just a regular console application. You can use the string[] args parameter from the Main method to retrieve any arguments that are passed to the application.
I would go look at this stack overflow question as it has links to some good command line parsing libraries in the answers.
One thing you'll need to do, if the application is to take any but the most basic arguments, is select a cmd line parsing library, as there is not one provided by the Framework.
I would look into the following link before making a significant command line application.
http://commandline.codeplex.com/
For question/edit #1:
If you add the path to the executable to your operating system's 'Path' environment variable, you can run the application from any directory. More info on setting it for Windows XP.
Also, you may need to retrieve the current working directory when the application is started. I believe you can use Environment.CurrentDirectory to get this. I'd suggest saving it in case you need to do something that might change the current directory for your process.
For question/edit #2:
You can use something like this to parse the actual arguments that are passed into the application via the args string array. However, what you do with the arguments after that point is completely up to you. There's nothing stopping you from just going through the array and dealing with the arguments manually (foreach (string arg in args) {...}) if you prefer.
If you want to make the first command line argument the path to a file to write to, so be it. If you want to have an optional argument like /verbose then that works as well. You are really only limited to your imagination (and the limits of what can be legally typed on the command line :) )

Categories

Resources