shubhendu
shubhendu

Reputation: 223

Communication b/w two different windows application in c#

There are 2 completely different c# windows applications(for desktop). One application is generating a CSV(with data) each time when "start" button clicks. Now I need to send this CSV to another c# application to get it process through. OnClick of "start" button following things should happen in sequence:

  1. Create a CSV(with output data, and this data is generated by first application)
  2. Created CSV should pass through a function in second application.
  3. CSV Data should process in that function in second application.

I am able to create CSV from first application(1st step is DONE, please suggest for rest?)

Upvotes: 0

Views: 114

Answers (2)

Markus
Markus

Reputation: 22501

As you cannot change the second application, your only option is to use what it already supports. You stated in the comments that it can accept the file name as a command line parameter. So the (probably only) way is to make the first application start the second one and supply the path to the CSV file in the command line.

To begin with, I'd create the CSV file with the first application and store it in a folder that both applications can access. After that you can use a command prompt window to start the second application and supply the file path (and other necessary parameters as well). Once you are able to start the second application, you can extend the first application to create the process.

The main class is the Process class. The following is an example that you need to transfer to your situation:

var csvPath = System.IO.Path.GetTempFileName(); // Use a temporary file
// ... Create CSV
try
{
    var pathToSecondApp = "C:\\SecondAppFolder\\SecondApp.exe";
    var psi = new ProcessStartInfo(pathToSecondApp);
    psi.Arguments = "-file \"" + csvPath + "\""; // command line arguments for the process
    using(var p = Process.Start(psi))
    {
        // Wait for process to complete
        p.WaitForExit();
        // Analyze return code 
        if (p.ExitCode != 0)
            throw new ApplicationException("Error running Second App");
    }
}
finally
{
    // Delete CSV
    System.IO.File.Delete(csvPath);
}

Upvotes: 1

Ilshidur
Ilshidur

Reputation: 1521

You can try to extract the function from your 2nd application to a librairy project (.dll file). After importing this DLL in both of your applications you will be able to use the function in your first application.

To import the .dll file in your project, just go the the "References" tab in your project settings and choose "Add a Reference ...".

This is not a real communication between two applications, but it can solve your problem if you only want an application behavior like you described it.

Upvotes: 0

Related Questions