safaa
safaa

Reputation: 1

Problem with running exe file from c#

When I execute an exe file (PVFProject15.exe), it reads the data from an input file (inputFile.txt) and print the results in another file (outputFile.txt). The exe file works well when I double click it; It opens the console window which stays opened until the output file is created. However, when I run (PVFProject15.exe) from c#, the console window opens and closes very quickly and the output file is never created.

I would really appreciate your help since I have been working to fix this for a whole day and never found the answer. Here is my code below.

private void button1_Click(object sender, EventArgs e)

{
        Process runFortran = new Process();
        try
        {
            runFortran.StartInfo.FileName = "C:\\temp\\trial\\PVFProject15.exe";
            runFortran.Start();
            runFortran.WaitForExit(); 
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }
    }

Thank you in advance.

Safaa

Upvotes: 0

Views: 1407

Answers (3)

smartsolution
smartsolution

Reputation: 1

I also meet with same problem, when I try start some .exe and .hta from my C# based software. I start to looking for solution and answer of Mike Mozhaev get to me right direction. In your code you need to use: StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));

So code have to be like this:

 if (File.Exists(appPath))
                {
                    Process runProcess = new Process();
                    runProcess.StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));
                    runProcess.StartInfo.UseShellExecute= true;
                    runProcess.StartInfo.FileName = appPath;
                    runProcess.Start();

                }

Upvotes: 0

Mike Mozhaev
Mike Mozhaev

Reputation: 2415

Probably PVFProject15.exe needs current directory to be set to C:\temp\trial

Upvotes: 1

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

If PVFProject15.exe writes to file using relative path, look for outputFile.txt in directory from which you start your main program-bootstrapper.

Upvotes: 0

Related Questions