2013-01-03 34 views
3

我需要從C#自動化外部Windows控制檯應用程序。應用程序基本上是與外部設備的接口。當我調用應用程序時,它會要求我進行身份驗證,即輸入密碼並提示類似「輸入密碼:」的內容。現在沒有辦法配置此應用程序在沒有交互式密碼提示的情況下運行。發送密碼/字符串到外部控制檯應用程序C#

所以我想通過發送密碼在C#中自動執行同樣的操作,然後觸發將在外部設備上執行的come命令,然後抓取輸出。我知道流程類,我有一些像我可以使用管道這樣的指針(不確定?)。

由於本人在尋求幫助/方向之前沒有處理過這種自動化操作,

在此先感謝。

回答

5

做到這一點的方法是使用重定向成員,e.g ProcessStartInfo.RedirectStandardInput Property

 Process myProcess = new Process(); 

    myProcess.StartInfo.FileName = "someconsoleapp.exe"; 
    myProcess.StartInfo.UseShellExecute = false; 
    myProcess.StartInfo.RedirectStandardInput = true; 
    myProcess.StartInfo.RedirectStandardOutput = true; 
    myProcess.StartInfo.ErrorDialog = false; 

    myProcess.Start(); 

    StreamWriter stdInputWriter = myProcess.StandardInput; 
    StreamReader stdOutputReader = myProcess.StandardOutput; 

    stdInputWriter.WriteLine(password); 

    var op = stdOutputReader.ReadLine(); 

    // close this - sending EOF to the console application - hopefully well written 
    // to handle this properly. 
    stdInputWriter.Close(); 


    // Wait for the process to finish. 
    myProcess.WaitForExit(); 
    myProcess.Close(); 
相關問題