gpt4 book ai didi

c# - 打开 "known file type"进入自定义应用程序的运行实例 - .NET

转载 作者:太空狗 更新时间:2023-10-29 18:13:00 26 4
gpt4 key购买 nike

您如何将一个文件(在注册表中具有已知的文件/应用程序关联)打开到它应该在其中打开的应用程序的“运行实例”中?一个例子是,我打开了 Excel,然后单击 XLS 文件.....该文件在当前 Excel 实例中打开。我想为自定义应用程序执行此操作...“告诉”当前实例它需要打开文件的事件/消息传递如何工作?是否有一个“文件观察器”寻找这样做的请求等?谢谢..

最佳答案

你想要做的是从WindowsFormsApplicationBase继承一个类, 设置 protected IsSingleInstance属性为真:

// This should all be refactored to make it less tightly-coupled, obviously.
class MyWindowsApplicationBase : WindowsFormsApplicationBase
{
internal MyWindowsApplicationBase() : base()
{
// This is a single instance application.
this.IsSingleInstance = true;

// Set to the instance of your form to run.
this.MainForm = new MyForm();
}
}

您的应用程序的 Main 方法如下所示:

// This should all be refactored to make it less tightly-coupled, obviously.
public static void Main(string args[])
{
// Process the args.
<process args here>

// Create the application base.
MyWindowsApplicationBase appBase = new MyWindowsApplicationBase();

// <1> Set the StartupNextInstance event handler.
appBase.StartupNextInstance = <event handler code>;

// Show the main form of the app.
appBase.Run(args);
}

请注意标记为 <1> 的部分。您使用 StartupNextInstanceEvent 的事件处理程序进行设置.当您有单个实例应用程序(您在 MyWindowsApplicationBase 的构造函数中指定)时,当您的应用程序的 下一个 实例被触发时,将触发此事件。事件处理程序将传递一个 EventArgs 派生类,该类将具有命令行参数,然后您可以在应用程序的运行实例中处理这些参数。

然后,您所要做的就是为您希望您的应用程序处理的文件类型正常设置文件关联,然后就可以了。

关于c# - 打开 "known file type"进入自定义应用程序的运行实例 - .NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/424368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com