Skip to content

cStdIO 使用示例

示例 1:获取系统信息

vb
Private Sub GetSystemInfo()
    Dim StdIO As New cStdIO
    Dim Output As String
    Dim ExitCode As Long
    
    ' 获取网络配置
    Output = StdIO.ExecuteCommand("ipconfig /all", 10000, "", ExitCode)
    
    If ExitCode = 0 Then
        txtOutput.Text = Output
    Else
        MsgBox "获取网络信息失败"
    End If
End Sub

示例 2:Git 操作

vb
Private Sub CheckGitStatus()
    Dim StdIO As New cStdIO
    Dim Result As String
    Dim ExitCode As Long
    
    ' 检查 Git 状态
    Result = StdIO.ExecuteCommand("git status", 5000, txtProjectPath.Text, ExitCode)
    
    If ExitCode = 0 Then
        ' 解析状态输出
        If InStr(Result, "nothing to commit") > 0 Then
            lblStatus.Caption = "工作区干净"
        Else
            lblStatus.Caption = "有未提交的更改"
        End If
        txtDetails.Text = Result
    Else
        MsgBox "Git 命令执行失败"
    End If
End Sub

示例 3:批量处理文件

vb
Private Sub BatchConvertFiles()
    Dim StdIO As New cStdIO
    Dim FileList As String
    Dim ExitCode As Long
    Dim Files() As String
    Dim i As Integer
    
    ' 获取文件列表
    FileList = StdIO.ExecuteCommand("dir /b *.txt", 5000, txtFolder.Text, ExitCode)
    
    If ExitCode = 0 And Len(FileList) > 0 Then
        Files = Split(FileList, vbCrLf)
        
        For i = LBound(Files) To UBound(Files)
            If Len(Files(i)) > 0 Then
                ' 处理每个文件
                ProcessFile txtFolder.Text & "\" & Files(i)
            End If
        Next i
    End If
End Sub

示例 4:网络检测

vb
Private Sub PingHost()
    Dim StdIO As New cStdIO
    Dim Host As String
    Dim Result As String
    Dim ExitCode As Long
    
    Host = txtHost.Text
    
    ' 执行 ping 命令
    Result = StdIO.ExecuteCommand("ping -n 4 " & Host, 20000, "", ExitCode)
    
    txtResult.Text = Result
    
    ' 解析结果
    If InStr(Result, "Reply from") > 0 Then
        lblStatus.Caption = "主机可连接"
        lblStatus.ForeColor = vbGreen
    Else
        lblStatus.Caption = "主机不可达"
        lblStatus.ForeColor = vbRed
    End If
End Sub

示例 5:执行带超时的任务

vb
Private Sub RunWithTimeoutControl()
    Dim StdIO As New cStdIO
    Dim Result As String
    Dim ExitCode As Long
    Dim StartTime As Long
    
    StartTime = Timer
    
    On Error GoTo TimeoutHandler
    
    ' 执行可能耗时的任务(30秒超时)
    Result = StdIO.ExecuteCommand("data-processing.exe", 30000, "", ExitCode)
    
    If ExitCode = 0 Then
        MsgBox "处理完成,耗时:" & (Timer - StartTime) & "秒"
        txtOutput.Text = Result
    End If
    
    Exit Sub
    
TimeoutHandler:
    If Err.Number = 5 Then  ' 假设超时错误号为 5
        MsgBox "任务执行超时(超过30秒)!", vbExclamation
    Else
        MsgBox "执行出错:" & Err.Description, vbCritical
    End If
End Sub

示例 6:命令行工具封装

vb
' 封装 FFmpeg 调用
Private Function RunFFmpeg(InputFile As String, OutputFile As String) As Boolean
    Dim StdIO As New cStdIO
    Dim Command As String
    Dim Output As String
    Dim ExitCode As Long
    
    ' 构建命令
    Command = "ffmpeg -i """ & InputFile & """ """ & OutputFile & """
    
    ' 执行(60秒超时)
    Output = StdIO.ExecuteCommand(Command, 60000, "", ExitCode)
    
    ' 判断是否成功
    RunFFmpeg = (ExitCode = 0)
    
    If Not RunFFmpeg Then
        Debug.Print "FFmpeg 错误:" & Output
    End If
End Function

VB6及其LOGO版权为微软公司所有