简体   繁体   中英

How do I pass a command to the command prompt using Visual C#?

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. 贝壳

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.

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();
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM