简体   繁体   中英

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

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

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();

                }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM