简体   繁体   中英

System.Diagnostics.Process: when I run a C++ program that creates an output file and writes something, the output file is not created

When I run a C++ program that creates an output file and writes something, the output file is not created, although the program works fine when I simply double click it from Windows Explorer.

This is the C# code I use to run the program:

            try
            {
                Process p = StartProcess(ExecutableFileName);
                p.Start();
                p.WaitForExit();
                Log("Program finished in " + ((p.ExitTime - p.StartTime).Milliseconds / 1000m) + " seconds with code " + p.ExitCode + "\n");
            }
            catch
            {
                Log("The program couldn't be started.");
            }

UPDATE

I just found out why it's happening.

Apparently, when I launch it with C#, the C++ program doesn't see the input file in the relative directory, but when I explicitly specify it

ifstream in("C:\\Alex\\primes.in");

it gets it and everything works! Now I need to make it work with relative file paths...

Here is the summary of our discussion of the problem. It turned out that the output file was in the C# program's debug folder, not in the directory where the C++ application was and the output was expected. The problem is solved by specifying the Working directory property of the project.

You must call Close() on process like so:

            try
            {
                Process p = StartProcess(ExecutableFileName);
                p.Start();
                p.WaitForExit();

                //THIS HERE
                p.Close();                    

                Log("Program finished in " + ((p.ExitTime - p.StartTime).Milliseconds /1000m) + " seconds with code " + p.ExitCode + "\n");


            }
            catch
            {
                Log("The program couldn't be started.");
            }

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