I have program written in C with is getting some data from device and save it on hard drive, then I need to proceed with that data from my C# project. What is the best way to pass that file/filestream/event between this projects?
-
Depends on the C# program! Is it just a background app, or a forms app, or a Windows service, or a web app, or something else? You can use FileSystemWatcher to monitor the output directories for new files, or you can probably IPC, or you can use some other monitoring mechanism based on the type of app. – Rup Mar 16 '15 at 15:29
-
It is background app, my goal is (if it is possible) to don't want to monitor folder, maybe send some event from C to C# – Carlos28 Mar 16 '15 at 15:31
4 Answers
Many possible solutions.
Simplest: If by "My C# project" you mean a Commandline exe written in C#: Just add a commandlöine argument to your C# program and call it form your C program.
Simple: Create a very simple file exchange protocol as follows: Invent a well known directory. The C program writes a well known file like "ExchangeFileName.txt" into this directory. The C# program regularily reads the wll known directory, waiting for the well known file to appear. Then it reads the pathname inside this file and knows what to do.
For Hackers: Make the C# program listen on a socket port. Let the C program write a message to this port.
For Experts: Use named pipes to send information from one .exe to the other. See Example of Named Pipes and MSDN: How to: Use Named Pipes for Network Interprocess Communication
One Option is to use MemoryMapped Files. You can either use persisted or non-persisted memory mapped files
Your other bet is to use FileSystemWatcher to monitor a certain directory for file changes.

- 61,549
- 15
- 193
- 205
How to tell another process that you have written a file. Its hard to say since you dont say much (are the 2 programs always running, or should the c# program be started when the file is ready,...)
You could use the file system watcher to watch for changes in the contents of a directory https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx
You could have a TCP connection open between them. C program sends a message when it writes a file
You could have the C program launch the c# program
Use System Wide Events (see Win32 call CreateEvent)

- 48,078
- 23
- 82
- 145
-
-
as for "You could have the C program launch the c# program" it can be posible, what then? – Carlos28 Mar 16 '15 at 15:38
-
i mean the c program writes a file, starts the c# program passing in the file name. The c# program then finishes – pm100 Mar 16 '15 at 15:43
So according to yours answers and some digging what I did:
Process p = Process.Start(@"C:\...\example.exe");
p.OutputDataReceived += p_OutputDataReceived;
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
but this event didn't work correctly, desc:
Occurs when an application writes to its redirected StandardOutput stream.
so std::cout StandardOutput stream?
@edit this did the trick: https://msdn.microsoft.com/pl-pl/library/system.diagnostics.process.outputdatareceived%28v=vs.110%29.aspx

- 2,381
- 3
- 21
- 36