简体   繁体   English

如何使用Visual C#将命令传递到命令提示符?

[英]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. 我正在创建一个Visual C#应用程序,该应用程序在命令提示符下填写正确的参数以检索iTunes销售和趋势数据。 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. 如果有一种方法可以在按Enter键之前在命令行中显示文本,以自动运行命令,那就太好了。 Any feedback would be appreciated. 对于任何反馈,我们都表示感谢。

If you are trying to run a cmd and write to it then this should do it: 如果您尝试运行cmd并对其进行写入,则应这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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