0

so I have a project that I'm working on which is basically a GUI implementation of a CLI program. The program works like this: program.exe LINE1 LINE2 > output.txt. In C# I have made the code to launch the program with inputs from 2 textboxes like this:

using (Process process = new Process())
                {
                    process.StartInfo.FileName = "c:\\users\\***\\desktop\\rar.exe";
                    process.StartInfo.Arguments = RarName + RarType + " > C:\\users\\***\\desktop\\app2.txt";
                    process.Start();
                }

It's purpose is to get 2 lines of texts from 2 textboxes and run them with the argument Textbox1 Textbox2 > outputfile.txt but as I said, it does not work. When I do the exact same thing by CMD manually, it works so the program isn't broken.

I am using variables, here is the code i use to make the variables have the same value as the textboxes:

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            RarName = textBox1.Text;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            RarType = textBox2.Text;
        }

Also at the beginning of the code, I set the variables to blank with this:

   public partial class Form1 : Form
   {
       string RarName = "";
       string RarType = "";
       public Form1()
       {
           InitializeComponent();
       }
user123
  • 31
  • 5
  • Rarname + rartype - don't you need a space between there? – Hans Kesting Sep 26 '22 at 19:38
  • Are you intending to enclose the text of LINE1 and LINE2 in quotes and separate them by a space for the purposes of passing each as a single argument? Or are you hoping that `RarName + RarType + " > C:\\users\\***\\desktop\\app2.txt"` will do the right thing based on either `RarName` having a space at the end or `RarType` having a space at the beginning? I suspect you may just need a space. between `RarName + " " + RarType` – Wyck Sep 26 '22 at 19:42
  • 2
    You don't use the output redirection symbol (`>`) when you use `Process.Start`. Look at https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output – Flydog57 Sep 26 '22 at 19:44
  • Also, the `>` is a feature of the command shell, not something that happens for free as an argument to a process. Redirecting IO of a process to a file is [more complicated](https://stackoverflow.com/questions/16256587/redirecting-output-to-the-text-file-c-sharp) – Wyck Sep 26 '22 at 19:44
  • "program recieves input like this" - just to clarify - program (when launched from a shell like CMD) will not receive `> output.txt` part of the command line. You may want to [edit] post to clarify what you actually want to achieve. – Alexei Levenkov Sep 26 '22 at 20:08
  • @AlexeiLevenkov sorry, fixed – user123 Sep 27 '22 at 16:18

0 Answers0