C#中单实例应用程序中的多个文件

C#中单实例应用程序中的多个文件

问题描述:

我有一个用Visual Studio c#编写的应用程序,它允许用户创建,编辑和保存特定类型的文件(类似于Microsoft Excel的功能)。我创建了一个文件关联,将文件扩展名链接到应用程序。



我最初的要求是双击带有相关扩展名的文件会在应用。我在 C#中的单实例应用程序中找到了一种清晰解释的方法[ ^ ],有效正确。



但是,如果应用程序已经运行,加载了文件,并且我用不同的文件重复练习,则新文件不会加载到应用程序。我希望功能类似于Microsoft Excel。



有谁知道怎么做?



另一个小问题:我正在运行Windows 10.双击文件会打开一个对话框,询问如何打开此文件,我的应用程序位于列表的开头。我必须双击应用程序名称才能打开它。如何让它直接打开应用程序?

I have an application written in Visual Studio c# which allows the user to create, edit and save files of a particular type (similar to the way Microsoft Excel functions). I have created a file association linking the file extension to the application.

My initial requirement was that double-clicking on a file with the relevant extension would open it in the application. I found a way of doing this clearly explained in Single Instance Application in C#[^], which works correctly.

However, if the application is already running, with a file loaded, and I repeat the exercise with a different file, the new file isn't loaded to the application. I want the functionality to be similar to Microsoft Excel.

Does anyone know how to do this?

Another minor point: I am running Windows 10. Double-clicking a file opens a dialog box asking "How do you want to open this file" with my application at the head of the list. I have to double-click the application name to open it. How can I make it directly open the application?

您没有提供任何相关信息,这些信息有助于查看您的应用程序中缺少的内容。但是您需要了解单实例行为是一回事,将数据从一个实例的命令行传递到另一个实例与此相关,但是有些不同。最有可能的是,您实现了单实例行为,并且根本无法处理您的文件。我不知道你为什么认为没有你的参与就应该这样做。我想如果你确实处理了你的文件,你会在你的问题中提到你尝试过的。



我有一个非常强大的解决方案,结合了两个功能使用同一设施。我在过去的答案中解释了所有的想法:

自定义Windows右键单击命令启动多个实例

仅向一个流程实例输入多个命令



另请参阅:

在已经打开的程序中打开文件 [ 2 ],

在已打开的程序中打开文件 [ 3 ]。



-SA
You did not provide any relevant information which would help to see what you are missing in your application. But you need to understand that single-instance behavior is one thing, and passing data from command line of one instance to another instance is related to that, but something very different. Most likely, you implemented single-instance behavior and simply did nothing to handle your files. I have no idea why did you assume that it should happen without your participation. I guess if you did take care of handling your files, you would mention what you tried in your question.

I have a very robust solution which combines both features using the same facility. I explain all the ideas in my past answers:
Custom Windows right-click command launching multiple instances,
Enter multiple commands to only one process instance.

See also:
Open File in already Open Program [2],
Open File in already Open Program [3].

—SA


使用TCP套接字的示例



1.启动TCPListener在表格上

2.在第二个实例的主要部分连接TCPClient

3.通过TCP连接发送ActivationArguments到表格



同时适用于多个文件,甚至第一次用于多个文件(当应用程序尚未启动时)



最重要的代码块是:



1. MainForm(Hoofdscherm)的构造函数,它启动服务器并将端口号写入文件。第一个文件也被打开。

2. Main-function(Program.cs),其中第二个,第三个......实例在第一个实例中连接到TcpListener并通过它发送文件名插座



源代码可在RAR格式的Broncode按钮上找到
Example using TCP-sockets

1. start TCPListener on the form
2. connect TCPClient in the main of the second instance
3. Send ActivationArguments through the TCP-connection to the form

Works for multiple files at once as well, and even for multiple files at the first time (when application not started yet)

The most important code blocks are:

1. The constructor of the MainForm (Hoofdscherm) where the server is started and the port number is written to a file. The first file is opened as well.
2. The Main-function (Program.cs) where the second, third, ... instance connects to the TcpListener in the first instance and sends the filename through the socket

The source code is available on the button "Broncode" in a RAR-format