对于 MS Word 类型的程序,WaitForExit - 进程已启动

发布于 2024-12-07 16:40:02 字数 555 浏览 0 评论 0原文

在我的应用程序中,我想打开 *.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文