0

我需要在 VB.net 中自动使用命令行实用程序。这是一个例子。

从代码中,我需要使用命令行实用程序解密文件。这是命令行过程。

您使用此行启动实用程序

C:\gnupg>gpg --decrypt c:\temp\File_Encr.xml

一旦执行,然后它显示这个

You need a passphrase to unlock the secret key for user: "xxxx <abc@def.com>" 1024-bit ELG-E key, ID ABCD, created 2013-10-25 (main key ID DEF)

输入密码:

当您输入密码时,它就可以完成工作。我需要从代码(VB.NET)开始这个过程并输入密码,这样它就不需要任何用户交互。我的代码将用于 Windows 服务以及 Web 应用程序。

有人可以帮忙吗?

谢谢你。萨默斯

4

1 回答 1

0

这是我使用的代码片段。

为方便起见,所有输出代码都写入 Visual Studio 调试窗口。所有程序输出都重定向到输出处理程序。这使您可以“实时”检查程序的输出。如果您需要查看输出行并扫描某个短语然后执行操作,您可以在 Sub OutputReceivedHandler() 中轻松完成此操作。

我试图让它通用,这样你就可以看到它是如何工作的:



    Imports Microsoft.VisualBasic
    Imports System.Diagnostics
    Imports System

    Public Class ExternalUtilities

        Private myprocess As Process
        Private SW As System.IO.StreamWriter
        Private myprocess_HasError As Boolean = False
        Private myprocess_ErrorMsg As String = ""
        Private myprocess_Output As String = ""

        Public Sub New()
            ' do init stuff here.
            ' maybe pass the executable path, or command line args.
        End Sub

        Public Sub launchUtilityProcess1()

            Dim executeableFullPath As String = "C:\Path\To\file.exe"

            ' define the process
            myprocess = New Process
            myprocess.StartInfo.FileName = executeableFullPath
            myprocess.StartInfo.RedirectStandardInput = True
            myprocess.StartInfo.RedirectStandardOutput = True
            myprocess.StartInfo.RedirectStandardError = True
            myprocess.StartInfo.UseShellExecute = False
            myprocess.StartInfo.CreateNoWindow = True
            myprocess.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(executeableFullPath)
            myprocess.StartInfo.Arguments = "--decrypt c:\temp\File_Encr.xml"

            ' add handlers to monitor various conditions
            myprocess.EnableRaisingEvents = True
            AddHandler myprocess.OutputDataReceived, AddressOf OutputReceivedHandler
            AddHandler myprocess.ErrorDataReceived, AddressOf ErrorReceivedHandler
            AddHandler myprocess.Exited, AddressOf ExitedHandler

            ' launch
            Try
                myprocess.Start()

                ' redirect this processes IO
                SW = myprocess.StandardInput
                SW.AutoFlush = True

                ' use asynchronous reading so buffers dont fill up
                myprocess.BeginOutputReadLine()
                myprocess.BeginErrorReadLine()

                ' wait for program to end
                myprocess.WaitForExit()
                myprocess.Close()
                SW.Close()
                myprocess.Dispose()

                ' check for errors
                If myprocess_ErrorMsg  "" Then
                    ' something bad happened, handle it.
                End If

            Catch ex As Exception
                ' something bad happened, handle it.
            End Try


        End Sub

        Private Sub OutputReceivedHandler(ByVal sendingProcess As Object, ByVal line As DataReceivedEventArgs)

            If Not line.Data Is Nothing Then

                If line.Data = "string to search for..." Then
                    ' when this string is detected, send more output
                    SW.Write("helloworld" & System.Environment.NewLine)
                ElseIf line.Data = "something else..." Then
                    ' when this string is detected, send more output
                    SW.Write("goodbyeworld" & System.Environment.NewLine)
                End If

                Debug.WriteLine(line.Data)
            End If

        End Sub

        Private Sub ErrorReceivedHandler(ByVal sendingProcess As Object, ByVal line As DataReceivedEventArgs)

            Debug.WriteLine(line.Data)

            '   These executables send newlines on the STDERR path, ignore newlines
            If line.Data <> "" Then
                myprocess_HasError = True
                myprocess_ErrorMsg = line.Data
            End If

        End Sub

        Private Sub ExitedHandler(ByVal sender As Object, ByVal e As System.EventArgs)
            Debug.WriteLine("Process Exited")
        End Sub

    End Class


于 2013-10-31T10:49:14.583 回答