字符串数组中的ProcessWindowStyle.Hidden

时间:2015-02-25 15:08:56

标签: arrays vb.net

在我的VB.net项目中,我试图使用数组调用多个程序来帮助清理我的代码。

目前,我有这段代码:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles StartServer.Click
    Dim proc As New ProcessStartInfo()
    Dim prochide As New ProcessStartInfo()
    prochide.WindowStyle = ProcessWindowStyle.Hidden
            If CheckBox1.CheckState = 1 Then
                proc.WorkingDirectory = TextBox1.Text
                proc.FileName = "xserver.exe"
                Process.Start(proc)
                proc.FileName = "yserver.exe"
                Process.Start(proc)
                proc.FileName = "zserver.exe"
                Process.Start(proc)
            Else
                prochide.WorkingDirectory = TextBox1.Text
                prochide.FileName = "xserver.exe"
                Process.Start(prochide)
                prochide.FileName = "yserver.exe"
                Process.Start(prochide)
                prochide.FileName = "zserver.exe"
                Process.Start(prochide)
            End If
     End Sub

这样做可以让我隐藏窗口,以便它们显示在任务管理器中,但窗口实际上并没有出现。

但是,我更愿意使用此代码或类似的东西切换它来清理它:

Dim servers(0 To 2) As String
servers(0) = "xserver.exe"
servers(1) = "yserver.exe"
servers(2) = "zserver.exe"

然后我可以简化代码:

 Dim directory As String = TextBox1.Text
                For Each fileName As String In servers
                Next

但是,我无法弄清楚如何隐藏数组中的窗口,因为.WindowStyle = ProcessWindowStyle.Hidden似乎不适用于数组字符串。有没有其他方法可以做到这一点?改变方法很好,我只是想尝试清理代码,因为它现在看起来有点笨重。

1 个答案:

答案 0 :(得分:0)

好的,所以我最终检查了这个,这对我有用:

Public Class Form1
    Dim servers() As String = New String() {"cmd.exe|1", "cmd.exe|1", "cmd.exe|1"}

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim directory As String = TextBox1.Text
        Dim prochide As New ProcessStartInfo()
        prochide.WorkingDirectory = directory
        For Each fileName As String In servers
            Dim FileOptions() As String = Split(fileName, "|")
            prochide.FileName = FileOptions(0)
            prochide.WindowStyle = CType(FileOptions(1), ProcessWindowStyle)
            Process.Start(prochide)
        Next
    End Sub
End Class

使用New String()时,无法使用限制声明数组。这就是我们的问题。 :)

因此,您必须使用Dim servers(3)...

而不是Dim servers(0 To 2)...Dim servers()...