比较vb.net中的两个For Each列表

时间:2018-11-04 15:52:43

标签: vb.net

我有一个小问题。 每个例程都有两个。一个会给我当前文件夹中的所有文件夹名称。另一个给我所有打开的窗口名称。两者都将结果保存在字符串中。一个带有换行符,另一个带有〜,因此我可以遍历两个行,并逐个获取所有项。

这是一部分:

    Dim Folders As String
    For Each Dir As String In System.IO.Directory.GetDirectories(My.Computer.FileSystem.CurrentDirectory & "\Data\")
        Dim dirInfo As New System.IO.DirectoryInfo(Dir)
        Folders = Folders & dirInfo.Name & "~"
    Next

    Dim FolderList() As String = Folders.Split("~")

    Dim p As Process
    Dim Windows As String
    For Each p In Process.GetProcesses
        Windows = Windows & vbNewLine & p.MainWindowTitle.ToString
    Next
    Windows = LineTrim(Windows)

这有效。但是现在,我想对它们进行比较。 我只想获取存在窗口的文件夹,其中包含文件夹名称。

例如,我有3个文件夹:Test1,Test2,Test3。 我打开了一个窗口:“ Test1-窗口”

现在,我只想一次获得“ Test1”作为结果。 到目前为止,我可以正常使用它,但是我得到了3次“ Test1”,因为有3个文件夹。因为我是根据此信息创建新Windows的,所以我的函数向新窗口发送了垃圾邮件。

这是整个功能:

Dim Folders As String
    For Each Dir As String In System.IO.Directory.GetDirectories(My.Computer.FileSystem.CurrentDirectory & "\Data\")
        Dim dirInfo As New System.IO.DirectoryInfo(Dir)
        Folders = Folders & dirInfo.Name & "~"
    Next
    Dim str As String() = Folders.Split("~")
    For Each Folder As String In str

        If (My.Computer.FileSystem.FileExists(My.Computer.FileSystem.CurrentDirectory & "\Data\" & Folder & "\" & "Status.txt")) Then
                Dim StartTime As String = Inireader.WertLesen("Settings", "StartTime", My.Computer.FileSystem.CurrentDirectory & "\Data\" & Folder & "\Time.ini")
                Dim StopTime As String = Inireader.WertLesen("Settings", "StopTime", My.Computer.FileSystem.CurrentDirectory & "\Data\" & Folder & "\Time.ini")
                If (IsInTime(StartTime, StopTime) = True) Then

                    Dim p As Process
                    Dim Windows As String
                    For Each p In Process.GetProcesses
                        Windows = Windows & vbNewLine & p.MainWindowTitle.ToString
                    Next
                    Windows = LineTrim(Windows)
                    Dim Ar() As String = Split(Windows, Environment.NewLine)
                    For Each Window As String In Ar
                        If sX.ToString.Contains(Window & "  - python  " & My.Computer.FileSystem.CurrentDirectory & "\Data\" & Window& "\" & Window & ".py") Then ''''The Spam cause line
                        Else
                            Dim Path = My.Computer.FileSystem.CurrentDirectory & "\Data\" & Folder & "\" & Folder & ".py"
                            Dim startInfo As New ProcessStartInfo
                            startInfo.FileName = "cmd.exe"
                            startInfo.Arguments = "/k " & "title " & Folder & " & python " & Path
                            startInfo.WorkingDirectory = My.Computer.FileSystem.CurrentDirectory & "\Data\" & Folder & "\"
                            Process.Start(startInfo)

                        End If
                    Next
                End If
        End If
    Next

我不能大大缩短它。 你能帮我吗? 谢谢 :) 最好的问候!

0 个答案:

没有答案