code-geek
code-geek

Reputation: 463

Cannot open the output file generated from running an exe

I have to run an exe file through C# code and open the output file generated by the exe to do some pre-processing.

The output file is generated successfully by running the exe. But when I try to open the output file at the same run, I am getting File Not Found Exception but when I run the program again, the code reads my output file and I am able to do the pre-processing.

private static void launchExe()
{
    string filename = Path.Combine("//myPathToExe");
    string cParams = "argumentsToExe";
    var proc = System.Diagnostics.Process.Start(filename, cParams);
    proc.Close();
}

I now need to open the output file generated by the exe.

private static void openOutputFile()
{
    StreamReader streamReader = new StreamReader("//PathToOutputFile");
    string content = streamReader.ReadToEnd();

    /*
     * Pre Processing Code
     */            
}

At this stage I am getting File Not Found Exception but I have the output file generated in the specified path.

Kindly help me with this issue.

Upvotes: 1

Views: 183

Answers (1)

Access Denied
Access Denied

Reputation: 9491

Don't close your app, but WaitForExit.

string filename = Path.Combine("//myPathToExe");
string cParams = "argumentsToExe";
var proc = System.Diagnostics.Process.Start(filename, cParams);
proc.WaitForExit();

Upvotes: 4

Related Questions