views:

302

answers:

4

Related question [stackoverflow] here.

I'm trying to do the above, but I want to take the process one step further. I want to open an arbitrary file using the default editor for the file type. From that point, I want to allow my user to interact with the file as they would normally, or continue to work in my application. The extension is what happens after the user finishes editing. Is there a way I can capture a close (and ideally save) event from the external application and use that as a trigger to do something else? For my purposes, tracking the closing of the external application would do.

I can do this in the specific case. For example, I can open a Word instance from my application and track the events that interest my application. However, I want to de-couple my application from Word.I want to allow my users to use any document editor of their choice, then manage the storage of the file being edited discretely behind the scenes.

A: 

To listen for file change, you can use the FileSystemWatcher and listen for a change in the last modified date.

You can also monitor the process and check then file when the process close.

Pierre-Alain Vigeant
+2  A: 

Use the FileSystemWatcher class to watch for changes to the file.

EDIT: You can also handle the Exited event of the Process object to find out when the program is exited. However, note that that won't tell you of the user closes your file but doesn't exit the process. (Which is especially likely in Word).

SLaks
+1 Handling the Exited event is better than using WaitForExit.
MusiGenesis
+2  A: 

You can do this in a manner similar to the referenced question, but the syntax is slightly different:

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = 
    new System.Diagnostics.ProcessStartInfo("C:\...\...\myfile.html");
process.Start();
process.WaitForExit(); // this line is the key difference

The WaitForExit() call will block until the other application is closed. You would use this code in a separate thread so that the user can keep using your application in the meantime.

MusiGenesis
A: 

I found this useful tip online just now. It seems to be what you looking for:

This website has some more detail and useful to the point tips on C# programming.

link to article: http://www.monkeycancode.com/c-run-process-wait-for-it-to-finish

link to website: monkeycancode.com

***string filename = "instruction.txt"; System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@filename);

System.Diagnostics.Process rfp = new System.Diagnostics.Process(); rfp = System.Diagnostics.Process.Start(psi);

rfp.WaitForExit(2000);

if (rfp.HasExited) { System.IO.File.Delete(filename); }

//execute other code after the program has closed MessageBox.ShowDialog("The program is done.");***