If I control both applications, what is the best way to communicate 开发者_开发知识库between 2 exe's written in VB.Net. For example, I want to drop an XML file from one app, and pick it up with the other, but I do not want poll for the file. I've heard of named pipes, but I found it was complicated. What's the most effecient way to do this?
The easiest way is probably to use Windows Communication Foundation. This article has example code written in VB.NET.
You don't have to poll for the file. Use a FileSystemWatcher.
One simple way would be to use WCF. The receiver application could host a simple WCF service, and the sender could send the file to it.
.NET 4 includes support for memory-mapped files. With these you may even eschew the need to use the filesystem. However, if the processes are not running on the same machine, you'll have to use some other approach (as mentioned by others, WCF would be a good one).
If you can edit the .exe’s file here is the easiest way:
Add a FileSystemWatcher Object in one of the .exe and set a Filter to a Specific file for example “Commands.txt”
FileSystemWatcher1.Path = Application.StartupPath
FileSystemWatcher1.NotifyFilter=NotifyFilters.LastWrite
FileSystemWatcher1.Filter = "Commands.txt"
FileSystemWatcher1.EnableRaisingEvents = True
To star/stop monitoring, set the path and the EnableRaisingEvents property to True or False
This is the Event Raised when the file changes:
Private Sub FileSystemWatcher1_Changed(sender As System.Object, e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
'Open the file and read the content.
'you can use your own commands
End Sub
This way, you only will get an event when the file changes, and no need to use timers or anything else.
The other .exe file, just have to write the commands or the message you want to send: This example writes the current datetime overwriting the file each time.
Dim Timestamp() As Byte = System.Text.Encoding.Default.GetBytes(Now.ToString)
Dim Fs As System.IO.FileStream
Fs = New System.IO.FileStream("Commands.txt", FileMode.Create, FileAccess.Write)
Fs.Write(Timestamp, 0, Timestamp.Length - 1)
Fs.Close()
Done!
精彩评论