0

我这里有问题。

        string strDir = @"cd /d d:\dirc";
        string strCmd = @"gpg --output d:\dirop\outputfile.TXT --decrypt d:\dirip\encfile.enc";  
        string strPass = TheWCF.GetPassphrase(); \\Which will return a string value.

        Process pr;
        ProcessStartInfo args = new ProcessStartInfo("cmd.exe");  
        args.RedirectStandardInput = true;
        args.RedirectStandardOutput = true;
        args.UseShellExecute = false;
        args.WindowStyle = ProcessWindowStyle.Normal;
        pr = Process.Start(args);

        pr.StandardInput.WriteLine(strDir);
        Application.DoEvents();
        Thread.Sleep(1000);
        Application.DoEvents();

        pr.StandardInput.WriteLine(strCmd);
        Application.DoEvents();
        Thread.Sleep(1000);
        Application.DoEvents();

到这里一切都很好,但此时 cmd 提示符会要求输入密码...如何从后面的代码中传递密码(密码我们从另一个我无法硬编码的服务中获得)例如:

 string strPassPhrase=TheWCF.GetPassphrase();

如何将此 strPassPhrase 传递给命令提示符?请建议一种方法提前谢谢

4

1 回答 1

0

This may be too obvious, but have you tried

pr.StandardInput.WriteLine(strPassPhrase);

From the Microsoft MSDN page:

A Process can read input text from its standard input stream, typically the keyboard. By redirecting the StandardInput stream, you can programmatically specify the input. For example, instead of using keyboard input, you can provide text from the contents of a designated file or output from another application. ref: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx

Actually, it seems like it's exactly what you want... as in this example (slightly modified from the reference linked above):

Dim myStreamWriter As StreamWriter = pr.StandardInput

     ' Prompt the user for input text lines to sort.  
     ' Write each line to the StandardInput stream of 
     ' the sort command. 
     Dim inputText As String 
     Dim numLines As Integer = 0
Do
        pr.WriteLine("Enter a line of text (or press the Enter key to stop):")
        inputText = pr.StandardInput.ReadLine()
        If inputText.Length > 0 Then
           numLines += 1
           myStreamWriter.WriteLine(inputText)
        End If 
     Loop While inputText.Length <> 0
于 2013-05-14T04:46:45.623 回答