0

i have exe file generated by the Compilation of C++ program now I want to pass arguments to the generated exe file through C# and save all of the output of exe file on a .txt file this is What i have done so far

public static string RunCmd(params int[] commands)
{
    string returnvalue = string.Empty;

    ProcessStartInfo info = new ProcessStartInfo("C:\\...\\.exe");
    info.UseShellExecute = false;
    info.RedirectStandardInput = true;
    info.RedirectStandardOutput = true;
    info.CreateNoWindow = true;

    using (Process process = Process.Start(info))
    {
        StreamWriter sw = process.StandardInput;
        StreamReader sr = process.StandardOutput;

        //foreach (int command in commands)
        //{
        //    sw.WriteLine(command);
        //}
        for (int i = 0; i < commands.Length; i++)
        {
            sw.WriteLine(commands[i]);
        }

        sw.Close();
        returnvalue = sr.ReadToEnd();
        process.Close();
        System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\..\\.txt");
        file.WriteLine(returnvalue);

        file.Close();



        return returnvalue;

}

it work well for a single argument but whenever i try to enter multiple Arguments,it just opens the .exe file and not work further

Hussain Ahmad
  • 71
  • 1
  • 9
  • Possible dup of http://stackoverflow.com/questions/3268022/process-start-arguments – Zuzlx Aug 26 '14 at 19:18
  • no i dont think so The exe file is generated by compilation of a C++ program which take two numbers as a input – Hussain Ahmad Aug 26 '14 at 19:20
  • Note that your sample does not pass "arguments" (like "my.exe arg1 arg2 arg3") but rather some input values. Please clarify what you actually looking for. Also make sure to link questions/articles about console redirect you've followed to get this code. – Alexei Levenkov Aug 26 '14 at 19:22
  • Hi Actually this code is working for a program that takes only one number as a input in its C++ program but whenever i try it on some other program that takes two numbers as a input .it doesnt work well and just opens that program .exe file i got the code from http://www.codeproject.com/Articles/79620/Run-console-application-from-a-NET-application?fid=1571242&select=3558834&tid=3558834 – Hussain Ahmad Aug 26 '14 at 19:27
  • upon function call i pass arguments like RunCmd(1,2); – Hussain Ahmad Aug 26 '14 at 19:28
  • Check out MSDN or SO for samples instead (like http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program) - likely you need to read and write in parallel, not closing input stream after first argument... (Side note: you need update your post to make it clear if you care about input redirection or command line arguments before someone try to post answer). – Alexei Levenkov Aug 26 '14 at 19:42
  • But why its not passing multiple arguments? and opens the .exe file – Hussain Ahmad Aug 26 '14 at 19:45
  • I haven't used this mechanism before, but it looks like it's because you're using WriteLine for each arg. I would think you'd want to format them all and write all at once. WriteLine is sending an end of line which the target program probably thinks it means your done with parameters. However, it probably depends on the target program. – Adam47 Aug 26 '14 at 20:22

0 Answers0