从 C# 打开任意应用程序

发布于 2024-08-04 05:05:10 字数 475 浏览 4 评论 0 原文

相关问题[stackoverflow]此处

我正在尝试执行上述操作,但我想更进一步。我想使用文件类型的默认编辑器打开任意文件。从那时起,我希望允许我的用户像平常一样与文件交互,或者继续在我的应用程序中工作。扩展是用户完成编辑后发生的事情。有没有办法可以从外部应用程序捕获关闭(最好是保存)事件并将其用作触发器来执行其他操作?出于我的目的,跟踪外部应用程序的关闭就可以了。

我可以根据具体情况执行此操作。例如,我可以从我的应用程序打开一个 Word 实例并跟踪我的应用程序感兴趣的事件。但是,我希望将我的应用程序与 Word 分离。我希望允许我的用户使用他们选择的任何文档编辑器,然后在幕后单独管理正在编辑的文件的存储。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℡寂寞咖啡 2024-08-11 05:05:10

您可以按照与引用的问题类似的方式执行此操作,但语法略有不同:

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

WaitForExit() 调用将阻塞,直到其他应用程序关闭。您可以在单独的线程中使用此代码,以便用户可以同时继续使用您的应用程序。

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.

隔岸观火 2024-08-11 05:05:10

使用 FileSystemWatcher 监视文件的更改。

编辑:您还可以处理退出 事件noreferrer">Process 对象来查找程序何时退出。但是,请注意,这不会告诉您用户关闭了您的文件,但也没有退出该进程。 (这在 Word 中尤其可能)。

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

幽蝶幻影 2024-08-11 05:05:10

要监听文件更改,您可以使用 FileSystemWatcher 并监听最后修改日期的变化。

您还可以监视进程并在进程关闭时检查并归档。

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.

↙厌世 2024-08-11 05:05:10

我刚刚在网上发现了这个有用的提示。这似乎就是您正在寻找的东西。 本文(链接已损坏) 有一些关于 C# 编程的更详细、有用、切中要害的技巧。

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

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.");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文