openning an application using a shell command

  • Thread starter Thread starter Ohad Young
  • Start date Start date
O

Ohad Young

Hi,

I need to open (launch) an external application from my winform application.
The application is not a dot.net application, for example the windows
calculator.

I'm using the System.Diagnostics.Process class, for example:
....
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.WorkingDirectory = @"C:\WINDOWS\system32\";
proc.StartInfo.Arguments = "some argument";
proc.StartInfo.FileName = "calc.exe";
proc.Start();
....

Is there a better way to do it?
How can I close the application programmatically?
Can I close it automatically when my application exits?

Thanks in advance, Ohad

--
Ohad Young
Medical Informatics Research Center
Ben Gurion University
Information System Eng
Office Phone: 972-8-6477160
Cellular Phone: 972-54-518301
E-Mail: (e-mail address removed)
 
Inline:

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
Ohad Young said:
Hi,

I need to open (launch) an external application from my winform application.
The application is not a dot.net application, for example the windows
calculator.

I'm using the System.Diagnostics.Process class, for example:
...
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.WorkingDirectory = @"C:\WINDOWS\system32\";
proc.StartInfo.Arguments = "some argument";
proc.StartInfo.FileName = "calc.exe";
proc.Start();
...

Is there a better way to do it?

No, not for the kind of control you want, I think; you've got it right.
How can I close the application programmatically?
proc.Kill();

Can I close it automatically when my application exits?

Sure, just call Kill() on some unload or exit event.
 
Ohad Young said:
Hi,

I need to open (launch) an external application from my winform application.
The application is not a dot.net application, for example the windows
calculator.

I'm using the System.Diagnostics.Process class, for example:
...
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.WorkingDirectory = @"C:\WINDOWS\system32\";
proc.StartInfo.Arguments = "some argument";
proc.StartInfo.FileName = "calc.exe";
proc.Start();
...

Is there a better way to do it?
How can I close the application programmatically?
Can I close it automatically when my application exits?

Thanks in advance, Ohad

--
Ohad Young
Medical Informatics Research Center
Ben Gurion University
Information System Eng
Office Phone: 972-8-6477160
Cellular Phone: 972-54-518301
E-Mail: (e-mail address removed)

Well certainly im doing the same as you to start a batch file. If you wanted
to stop the process a suggestion would be to move the

System.Diagnostics.Process proc

to the Class scope and put a listener on the close event of your main
form... so.

this.Closing+=new CancelEventHandler(MainForm_Closing);

private void MainForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

proc.Close();

}
 
Tam Inglis said:
Well certainly im doing the same as you to start a batch file. If you wanted
to stop the process a suggestion would be to move the

System.Diagnostics.Process proc

to the Class scope and put a listener on the close event of your main
form... so.

this.Closing+=new CancelEventHandler(MainForm_Closing);

private void MainForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

proc.Close();

}

I stand corrected, kill then close ;-) And probably dispose too on the proc
object if its a closing event.
 
Hi Ohad,

Thanks for posting in this group.
For simple application such as calculator, the main window stands for its
program life. So you can use Process.CloseMainWindow to require its main
window to close.
The behavior of CloseMainWindow is identical to that of a user closing an
application's main window using the system menu. Therefore, the request to
exit the process by closing the main window does not force the application
to quit immediately.
If CloseMainWindow fails, you can use Kill to terminate the process. Kill
is the only way to terminate processes that do not have graphical
interfaces.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top