0

I am creating a Visual C# application that fills in the correct parameters in the command prompt to retrieve iTunes Sales and Trends data. My goal is to pass a string to the command prompt but I've only gotten as far is to locating the right directory. Below is the code I currently have.

string argument = (@"c/ java Autoingestion Credentials.properties " + lblVenderID.Text + " " + ddlReportType.Text + " " + ddlDateType.Text + " " + ddlReportSubtype.Text + " " + txtDate.Text);

System.Diagnostics.ProcessStartInfo process = new System.Diagnostics.ProcessStartInfo();
        process.FileName = "cmd.exe";
        process.WorkingDirectory = "C:/iTunes Sales Report/AutoIngestion";
        System.Diagnostics.Debug.WriteLine(argument);
        process.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
        System.Diagnostics.Process.Start(process);

As you can in the picture, it locates to the directory that I hard coded in, but it does not display the string of text that actually runs the command I want it to run. shell

If there is a way to display the text in the command line before pressing enter to run the command automatically that would be great. Any feedback would be appreciated.

Alex K
  • 22,315
  • 19
  • 108
  • 236
YoungStamos
  • 101
  • 2
  • 11
  • Generating batch file and running it will display the command in the console (unless you turn echo off, e.g. by specifying `@` at the beginning of line in bat-file) prior executing command line tool (which you seems doing, right?). In batch file you can also have [confirmation logic](http://stackoverflow.com/q/1794547/1997232). – Sinatr Jun 09 '16 at 12:51
  • Have a look at the `/k` argument for cmd.exe – yaakov Jun 09 '16 at 13:05

1 Answers1

3

If you are trying to run a cmd and write to it then this should do it:

var processInfo = new ProcessStartInfo
                    {
                        FileName = "cmd.exe",
                        WorkingDirectory = "C:/iTunes Sales Report/AutoIngestion",
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        UseShellExecute = false,
                        CreateNoWindow = false
                    };

var process = new Process {StartInfo = processInfo };

process.Start();

// This will write your command and excute it
process.StandardInput.WriteLine(argument);

process.WaitForExit();

If you want to view the output, here is how:

string output = string.Empty;
string error = string.Empty;

//If you want to read the Output of that argument
using (StreamReader streamReader = process.StandardOutput)
      {
          output = streamReader.ReadToEnd();
      }

//If you want to read the Error produced by the argument *if any*
using (StreamReader streamReader = process.StandardError)
      {
          error = streamReader.ReadToEnd();
      }
Explisam
  • 749
  • 13
  • 26
  • Hey thank you for the response. I tried the code you gave me and for some reason the command prompt opens and closes very quickly and for the split second that I get to see the command prompt, I see nothing in there. Do you know why that is happening? – YoungStamos Jun 09 '16 at 13:12
  • Delete this line `process.StandardInput.WriteLine("exit");` i made an edit for it. – Explisam Jun 09 '16 at 13:21
  • I deleted this line and it still does the same thing unfortunately. Any other reason for this? – YoungStamos Jun 09 '16 at 13:24
  • What exactly is your `argument`? – Explisam Jun 09 '16 at 13:26
  • string argument = (@"c/ java Autoingestion Configuration.properties " + lblVenderID.Text + " " + ddlReportType.Text + " " + ddlDateType.Text + " " + ddlReportSubtype.Text + " " + txtDate.Text); – YoungStamos Jun 09 '16 at 13:28
  • Any changes needed to be made to this argument to display it in the command prompt maybe? I'm not sure. – YoungStamos Jun 09 '16 at 13:30
  • Looks like the maximized state is whats making the cmd to immediately shutdown .. try delete this `WindowStyle = ProcessWindowStyle.Maximized` – Explisam Jun 09 '16 at 13:31
  • I commented it out and it is still causing it to immediately shut down. Pretty strange. – YoungStamos Jun 09 '16 at 13:34
  • Well it should shutdown after a couple seconds when it's donw. Are meaning to see the output of the `cmd`? – Explisam Jun 09 '16 at 13:40
  • I want the command prompt to open and display the argument on the command line before executing it. I just want to write to the command line, is there any way to do that? – YoungStamos Jun 09 '16 at 13:47
  • Hey I wrote that code you gave me to view the output but I am getting an error saying "StandardError has not been redirected". I don't know what this means. – YoungStamos Jun 09 '16 at 13:55
  • Nevermind, I forgot to add ReidrectStandardError = true. The command prompt opens and stays but it is completely blank. It does not show the directory I want to locate nor the command I want to pass to it. – YoungStamos Jun 09 '16 at 14:05
  • Check the edit .. and as for what you'r asking for .. i don't think it is possible to just write to a `cmd` without executing. – Explisam Jun 09 '16 at 14:06
  • Read the argument you passed from the redirected input i just added above. – Explisam Jun 09 '16 at 14:08
  • I added the argument that reads the input I'm going to pass but I am getting an error saying " Cannot implicitly convert type 'System.IO.StreamWriter' to 'System.IO.StreamReader' " – YoungStamos Jun 09 '16 at 14:16