I never use C# .net 2005, How i can open .exe file that actualy run in DOS and get the data and print it on .txt file. the way is I write the names of .exe file and I press the button and I can see everything one time.

Dani AI

Generated

A quick, practical pattern that does what you asked: start the console EXE as a child process, capture its standard output and error while it runs, and write that captured text to a .txt file. was on the right track about launching an external process; the snippet below shows a robust, ready-to-use approach that also avoids the common deadlock pitfalls and works for showing output live or saving it when the process finishes.

using System;
using System.Diagnostics;
using System.IO;
using System.Text;

string exePath = @"C:\path\to\your.exe";
string args = ""; // any arguments
var psi = new ProcessStartInfo
{
    FileName = exePath,
    Arguments = args,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    CreateNoWindow = true,
    WorkingDirectory = Path.GetDirectoryName(exePath)
};

var output = new StringBuilder();

using (var p = new Process { StartInfo = psi })
{
    p.OutputDataReceived += (s, e) => { if (e.Data != null) output.AppendLine(e.Data); };
    p.ErrorDataReceived  += (s, e) => { if (e.Data != null) output.AppendLine(e.Data); };

    p.Start();
    p.BeginOutputReadLine();
    p.BeginErrorReadLine();
    p.WaitForExit();
}

File.WriteAllText(@"C:\temp\capture.txt", output.ToString());

Troubleshooting and important notes: if you get no output, the EXE might write directly to the console buffer (not stdout/stderr); in that case try running it through cmd.exe /c "prog.exe > out.txt 2>&1". For interactive programs that prompt for input, set RedirectStandardInput = true and write to p.StandardInput. If this runs inside ASP.NET, avoid blocking the request thread for long tasks and ensure the app pool identity has permission to run the executable — use a background job/service instead for reliability. For more detail and edge cases see Microsoft's guide on capturing process output and the ProcessStartInfo docs: and ProcessStartInfo.RedirectStandardOutput.

Recommended Answers

All 2 Replies

how far u do this?

I never use C# .net 2005, How i can open .exe file that actualy run in DOS and get the data and print it on .txt file. the way is I write the names of .exe file and I press the button and I can see everything one time.

write a batch file to execute the exe.
use System.diagnostics

create a new process pr
start the process to execute the batch file
write the out put to a string and print it where ever you want what ever the format you choose

thats it you got solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.