C # open arbitrary application

A related question is [stackoverflow] here .

I am trying to do this, but I want to take this step one step further. I want to open an arbitrary file using the default editor for the file type. From now on, I want my user to be able to interact with the file, as usual, or continue to work in my application. An extension is what happens after a user completes editing. Is there a way to capture the closing event (and ideally save) from an external application and use it as a trigger to do something else? For my purposes, tracking the closure of an external application would do.

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

+4
source share
4 answers

You can do this in a way similar to the above 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 

Call WaitForExit () is blocked until another application closes. You will use this code in a separate thread so that the user can continue to use your application in the meantime.

+3
source

Use the FileSystemWatcher class to monitor changes to the file.

EDIT . You can also handle the Exited Process event to find out when the program will exit. However, note that this will not tell you that the user is closing your file but is not exiting the process. (Which is especially likely in Word).

+2
source

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

You can also track the process and check the file when closing the process.

0
source

I found this helpful tip online now. This seems to be what you are looking for. This article (link broken) provides more detailed and useful C # programming tips.

 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."); 
0
source

All Articles