【问题标题】:VB.net Looped ReadLine stops after one lineVB.net Looped ReadLine 在一行后停止
【发布时间】:2016-02-06 00:18:11
【问题描述】:

好的,我在尝试执行循环时遇到了问题我有一个包含服务器列表的变量,当我尝试循环时它只循环一次并停止

我在下面有一部分代码,试图让您了解我在做什么,尽管我已经删除了任何可以指向我工作的公司的数据。

第一部分代码,从域中获取服务器列表

Dim oStartInfo As New ProcessStartInfo("c:\windows\system32\dsquery.exe", "computer " & cnameodd & oservpath & " -o rdn")
        oStartInfo.UseShellExecute = False
        oStartInfo.RedirectStandardOutput = True
        oStartInfo.RedirectStandardError = True
        oStartInfo.CreateNoWindow = True
        oProcess.StartInfo = oStartInfo
        oProcess.Start()
        Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
            sOutput = oStreamReader.ReadToEnd()

sOutPut 的输出如下所示(通过 debug.write("servers: " & sOutPut) 显示在下面的代码中

"SERVERxafe01"
"SERVERxafe02"
"SERVERxafe03"
"SERVERxafe04"
"SERVERxafe05"
"SERVERxafe06"

然后我试图让输出为每个服务器循环一个命令

If sOutput = "" Then
                    Debug.Write("No Servers Found")
                Else
                    Debug.Write("Servers: " & sOutput)
                    Dim reader As New StringReader(sOutput.Replace(Chr(34), ""))
                    While True
                        Dim line = reader.ReadLine()
                        Debug.Write("Line is" & line)
                        If line IsNot Nothing Then
                            Dim command As String = " user " & user & " /server:" & line
                            Dim pStartInfo As New ProcessStartInfo("c:\windows\sysnative\query.exe", command)
                            pStartInfo.UseShellExecute = False
                            pStartInfo.RedirectStandardOutput = True
                            pStartInfo.RedirectStandardError = True
                            pStartInfo.CreateNoWindow = True
                            pProcess.StartInfo = pStartInfo
                            pProcess.Start()
                            Using pStreamReader As System.IO.StreamReader = pProcess.StandardError
                                sOutput = pStreamReader.ReadToEnd()
                                Debug.Write(sOutput & "Error " & command)
                            End Using
                            Using pStreamReader As System.IO.StreamReader = pProcess.StandardOutput
                                sOutput = pStreamReader.ReadToEnd()
                                Debug.Write(sOutput & "Output" & command)
                                Return sOutput
                            End Using
                        End If
                    End While
                End If
            End Using

在代码中,我试图通过执行 debug.write 来输出它当前正在处理的行,但是每当我运行它时,我只看到 sOutPut 的第一行正在使用,没有其他行被循环,所以基本上只有输出debug.write("Line is :" & line) is

Line is : SERVERxafe01

所以我永远不会让它通过其他服务器循环>

我只是编写代码来尝试让我的工作更有效率,但我不认为自己是一名程序员,因为我很容易感到沮丧,并且在专注于代码时接听电话会让我有点前卫

欢迎提出任何想法,谢谢。

【问题讨论】:

    标签: vb.net string loops variables stringreader


    【解决方案1】:

    尝试替换

        while true
        '
        '
        '
        '
        '
        End While
    

        Do
        '
        '
        '
        '
        '
        Loop While line <> ""
    

    【讨论】:

    • 试过这个,得到相同的结果,它只在第一行循环一次并停止,调试仍然显示 sOutPut 有多行,但它只显示第一行正在完成一个循环。顺便说一句,使用 VB.net 2010
    【解决方案2】:

    在尝试查找更多信息后,我发现一篇文章展示了退出循环的不同技术,这不是我想要的,但在我的底部 using 语句中发现了一些引起我注意的东西

    Return sOutput
    

    退出模块所以它正在停止循环

    因此,经过修改,这现在可以正常工作了,我必须将 return 政治家放在 if 语句中,以便它仅在获取所需数据时才运行 return

      Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
            sOutput = oStreamReader.ReadToEnd()
            Dim server = sOutput.Replace(Chr(34), "")
            For Each line As String In server.Split(vbCrLf)
                Dim command As String = " " & user & " /server:" & line.Replace(vbLf, "")
                Dim pStartInfo As New ProcessStartInfo("c:\windows\sysnative\quser.exe", command)
                pStartInfo.UseShellExecute = False
                pStartInfo.RedirectStandardOutput = True
                pStartInfo.RedirectStandardError = True
                pStartInfo.CreateNoWindow = True
                pProcess.StartInfo = pStartInfo
                pProcess.Start()
                Using pStreamReader As System.IO.StreamReader = pProcess.StandardError
                    sOutput = pStreamReader.ReadToEnd()
                    If sOutput.Contains("SESSIONNAME") = True Then
                        Return "Found on Citrix Server: " & line & vbCrLf & sOutput
                    End If
                End Using
                Using pStreamReader As System.IO.StreamReader = pProcess.StandardOutput
                    sOutput = pStreamReader.ReadToEnd()
                    If sOutput.Contains("SESSIONNAME") = True Then
                        Return "Found on Citrix Server: " & line & vbCrLf & sOutput
                    End If
                End Using
            Next
        End Using
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      相关资源
      最近更新 更多