C
C#6d ago
roock89

Not getting the .txt output after running a process

Im trying to log all the text that is output from a program into a txt file, but after its run successfully it does not seem to generate the .txt file as specified, if it is generated i dont know where it is ending up. Im running the process like this:
string command = "info {0} -p {1} >> {2}_Pdal_PointLog.txt";
command = string.Format(command, path, pointCount-1, folderPath+filename);
//Execute the command
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = exePath,
Arguments = command,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};

process.StartInfo = startInfo;
process.Start();
string command = "info {0} -p {1} >> {2}_Pdal_PointLog.txt";
command = string.Format(command, path, pointCount-1, folderPath+filename);
//Execute the command
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = exePath,
Arguments = command,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};

process.StartInfo = startInfo;
process.Start();
the entire program is completing and exiting, the process is outputting the correct data im requesting with the command, but as said there is no .txt file in the specified location (it should be in the same folder as the input file). its as if its entirely ignoring the >> {2}_Pdal_PointLog.txt section of the command. I have also double checked that the .txt path is correct as im logging the final command so i can see it. i have both tried using the .exe i need directly and cmd then calling the .exe with arguments from that but that does not seem to make a difference.
3 Replies
Pobiega
Pobiega6d ago
the > to redirect output is part of the shell, not something you can do via Process you'll need to capture the output yourself, and write it to a file using C# hint: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=net-9.0#system-diagnostics-process-standardoutput
wasabi
wasabi5d ago
Or turn UseShellExecute to true. Which isn't always guarenteed to work.
jcotton42
jcotton425d ago
That means File Explorer, not a terminal shell.
The word "shell" in this context (UseShellExecute) refers to a graphical shell (similar to the Windows shell) rather than command shells (for example, bash or sh) and lets users launch graphical applications or open documents.
https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-diagnostics-processstartinfo-useshellexecute

Did you find this page helpful?