📜  从 vb.net 应用程序运行命令行 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:16.101000             🧑  作者: Mango

代码示例2
Public Module Extensions
''' 
''' Extension method to run string as CMD command.
''' 
''' [String] Command to run.
''' [Boolean](Default:False) Option to show CMD window.
''' [Boolean](Default:False) Option to wait for CMD process to complete before exiting sub.
''' [Boolean](Default:False) Option to keep window visible after command has finished. Ignored if ShowWindow is False.

Public Sub RunCMD(command As String, Optional ShowWindow As Boolean = False, Optional WaitForProcessComplete As Boolean = False, Optional permanent As Boolean = False)
    Dim p As Process = New Process()
    Dim pi As ProcessStartInfo = New ProcessStartInfo()
    pi.Arguments = " " + If(ShowWindow AndAlso permanent, "/K", "/C") + " " + command
    pi.FileName = "cmd.exe"
    pi.CreateNoWindow = Not ShowWindow
    If ShowWindow Then
        pi.WindowStyle = ProcessWindowStyle.Normal
    Else
        pi.WindowStyle = ProcessWindowStyle.Hidden
    End If
    p.StartInfo = pi
    p.Start()
    If WaitForProcessComplete Then Do Until p.HasExited : Loop
End Sub
End Module