C# 程序间通信的两种方式

一  读写txt方式
多个程序同时读写一个txt时,按如下的方式,可避免txt被同时操作的问题。FileStream  fStream = new FileStream(strPath,XXX,FileAccess.ReadWrite,FileShare.ReadWrite)

FileShare Enumeration


Member nameDescription Delete

Allows subsequent deleting of a file.

Inheritable

Makes the file handle inheritable by child processes. This is not directly supported by Win32.

None

Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.

Read

Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.

ReadWrite

Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed.However, even if this flag is specified, additional permissions might still be needed to access the file.(什么叫做additionnal permissions?下面的代码中为FileShare.ReadWrite,若FileStream的实例没有close,其它进程不能访问txt文件

Write

Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.

//write
string txtFullPath = "F:\\Signal\\signal.txt";

FileStream fs = new FileStream(txtFullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sr = new StreamWriter(fs);
sw.WriteLine("Signal is out!");
sw.Close();
fs.Close();
//read
string txtPath1 = "F:\\Result\\result.txt";
FileStream stream = new FileStream(txtPath1, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamReader read = new StreamReader(stream);
string strRead = read.ReadToEnd();
stream.Seek(0, SeekOrigin.Begin);
stream.SetLength(0);// 将txt置空
stream.Close();
read.Close();


二  SendMessage方式
       Demo 下载链接: http://download.csdn.net/download/qq_20161893/10029669

       Demo中实现了两个窗体程序间互发消息

上面的Demo中是通过窗口标题栏的名称获取,通过这种方法,两个程序中的窗口句柄都可以正确获取。但是int processHandle = FindWindow(null, "XXX”) 对于公司的软件却返回0,暂原因不明。

换个思路,先通过程序名得到PID(XXX.exe为XXX,这个地方有个坑,vs调试启动,是XXX.vshost,而不是XXX),而后通过PID获取程序句柄。

using System.Diagnostics;
using System.Runtime.InteropServices;

Process[] localByName = Process.GetProcessesByName("XXX");
IntPtr processHandle1 = localByName[0].MainWindowHandle;


最终效果如下:点击发送,另一个程序收到消息后弹出对话框


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值