在同一过程中打开多个文件 [英] Open multiple files in same process

查看:207
本文介绍了在同一过程中打开多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我的应用程序使用Mdi,这种形式可以读取测试文件,并且我想以自己的形式读取每个文件.

示例:
我将打开txt文件名1.txt,我的项目将以form1打开此文件(我们现在处于进程1).之后,如果我们通过打开方式"打开其他txt文件,则该文件将打开,但将在其他进程中打开.

我正在使用此代码显示是否存在第二个进程的消息:

Hello,

I have application use Mdi this form reads test file and I want to read each file in his own form.

Example:
I''ll open txt file name 1.txt my project will open this file in form1 (we are now in Process no 1). After that if we open other txt file by "Open With" the file will open but in other Process.

I''m using this code to show message if second Process exist:

if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
{
       MessageBox.Show("application is already running");
}


但是我想要的不是显示消息,而是在Process1中以表格2打开第二个文件.

我希望我很清楚.


But what I want is not to show the Message but to open the second file in form 2 in Process1.

I hope I''m clear.

推荐答案

可以使用以下几种选项:

首先,您可以使用命名管道.如果应用程序无法连接,则意味着它是第一个实例,并将其自身设置为服务器.否则它将是已经在运行的实例的客户端,并传递相关信息.
http://www.switchonthecode.com/tutorials/interprocess-communication-using- named-pipes-in-csharp [ ^ ]

其次,您可以看看这些在进程之间共享数据的方法.
用于进程间同步和通信的AC#框架 [单实例C#应用程序-适用于.NET 2.0 [
There are several options to use:

First you could use named pipes. If the application cannot connect this means that it is the first instance and will setup itself as server. Otherwise it will be a client to an already running instance and pass the related information.
http://www.switchonthecode.com/tutorials/interprocess-communication-using-named-pipes-in-csharp[^]

Second you could have a look at these methods of sharing data between processes.
A C# Framework for Interprocess Synchronization and Communication[^]

Third would be a solution you could implement without much hassle. (it''s more than just the single-instance implementation)

Single-Instance C# Application - for .NET 2.0[^]

Good luck!


这篇关于在同一过程中打开多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆