簡體   English   中英

如何使用Visual C#將命令傳遞到命令提示符?

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

我正在創建一個Visual C#應用程序,該應用程序在命令提示符下填寫正確的參數以檢索iTunes銷售和趨勢數據。 我的目標是將字符串傳遞給命令提示符,但到目前為止,我只是找到了正確的目錄。 以下是我目前擁有的代碼。

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

正如您在圖片中可以看到的那樣,它位於我進行硬編碼的目錄中,但是它不顯示實際運行我希望它運行的命令的文本字符串。 貝殼

如果有一種方法可以在按Enter鍵之前在命令行中顯示文本,以自動運行命令,那就太好了。 對於任何反饋,我們都表示感謝。

如果您嘗試運行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();

如果要查看輸出,請按以下步驟操作:

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