Launch another application from within C# Winforms application

G

GrantS

Hi
I am trying to use ShellExecute to launch an application to display a
certain file.

The variation on the theme is that I need to be able to specify the
application to launch and not simply pass the file name (which will
then result in the application associated with the file extension to
launch). I want to prevent the application registered in the system as
being associated with the file extension from opeing the file.

For instance, if MSPaint is associated with "*.bmp" file on my system
but I want to programatically open a file (also having a "*.bmp"
extension) with, say, Photoshop, how can I achieve this?

So far, I am using:

System.Diagnostics.Process p = new Process();
p.StartInfo.RedirectStandardOutput=false;
p.StartInfo.FileName=fileToLaunch;
p.StartInfo.UseShellExecute=false;
p.Start();
p.WaitForExit();
p.Dispose();

But this does not permit me to nominate the application I wish to use
to display the file.

Hope someone can assist.

Thanks

Grant
 
G

Guest

Hi,

If you know the application exe name (and in some cases the path) you need
to launch, then it's usually only a case of passing the document to be opened
as an argument to ProcessInfo.
For eg:
ProcessStartInfo procInfo=new ProcessStartInfo("notepad", "c:\\a.txt");

Is this what you are looking for?

HTH,
Rakesh Rajan
 
D

deko

If you know the application exe name (and in some cases the path) you need
to launch, then it's usually only a case of passing the document to be opened
as an argument to ProcessInfo.
For eg:
ProcessStartInfo procInfo=new ProcessStartInfo("notepad", "c:\\a.txt");

I want to let users view an XML log file with notepad, so I tried something
almost identical to your example:

System.Diagnostics.ProcessStartInfo notepadLog = new
System.Diagnostics.ProcessStartInfo("notepad.exe", "c:\\myLogFile.xml")

But nothing happens - am I missing something? .. do I need something like:

Application.Run(notpadLog) ?
 
J

John M Deal

Try this:

System.Diagnostics.Process.Start("notepad.exe", "C:\\myLogFile.xml");

That should launch the application for you and return Process object for
you to work with. Then again you can also pass your ProcessStartInfo
object to the Start method (it has a few overloads). Hope that helps.

Have A Better One!

John M Deal, MCP
Necessity Software
 
D

deko

System.Diagnostics.Process.Start("notepad.exe", "C:\\myLogFile.xml");
That should launch the application for you and return Process object for
you to work with. Then again you can also pass your ProcessStartInfo
object to the Start method (it has a few overloads). Hope that helps.

That works great! Thanks!

I also tried:

System.Diagnostics.ProcessStartInfo notepadLog = new
System.Diagnostics.ProcessStartInfo("notepad.exe", "c:\\myLogFile.xml");

System.Diagnostics.Process.Start(notepadLog);

That works, too - but requires more typing...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top