From C#, open an arbitrary application

情到浓时终转凉″ 提交于 2019-12-04 11:30:53

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.

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).

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.

I found this useful tip online just now. It seems to be what you are looking for. This article (link broken) has some more detail and useful, to-the-point tips on C# programming.

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.");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!