对于 MS Word 类型的程序,WaitForExit - 进程已启动
在我的应用程序中,我想打开 *.rtf 文件,然后等待它关闭。通常用户使用 MS Word 打开 *.rtf 文件,这就是问题所在。下面的代码有效,但仅当“WINWORD”进程尚未启动时才有效。如果是这样,调用 Process.Start() 只会打开一个新的 Word 窗口,并且 Process 对象中的大部分数据都会变空。我无法“等待”进程,因为它抛出异常。我该如何处理?请帮忙。
Process p = new Process();
p.StartInfo.FileName = @"C:\Users\UserName\Desktop\MyFile.rtf";
p.Start();
string name = p.ProcessName;
p.WaitForExit();
Console.WriteLine(name + " has exited");
Console.ReadKey();
*编辑: 我分析了一些解决方案,并且我注意到,如果用户打开 *.rtf 文件的应用程序像 Word(可能在多个窗口中打开许多文件),我只需等待我的 *.rtf 文件窗口,而不是整个过程。这太愚蠢了。问题越来越复杂。请帮忙。
In my app I want to open *.rtf file, and than wait for it to be closed. Often user has MS Word to open *.rtf files and here is the problem. Code below works, but only when "WINWORD" process has not been started yet. When it is, calling Process.Start() opens only a new window of Word, and most of data from Process object becomes empty. I can't 'wait' for process, cuz it throws an exception. How can I deal with it? Please, help.
Process p = new Process();
p.StartInfo.FileName = @"C:\Users\UserName\Desktop\MyFile.rtf";
p.Start();
string name = p.ProcessName;
p.WaitForExit();
Console.WriteLine(name + " has exited");
Console.ReadKey();
*Edit:
I have analyzed some solutions, and I have noticed, that if application by which user opens the *.rtf file is like Word (may open many files in many windows), I have to wait only for my *.rtf file window, not whole Process. It would be stupid. The problem is more and more complicated. Please Help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论