阻止更多文件夹和应用程序

时间:2016-05-28 17:04:54

标签: vb.net

正如您在此处所见,我阻止了名为“cleo”的目录。如果我在我的文件夹中有它,我就无法点击连接。如何检查浏览文件中是否存在“Cleo”,“Images”,“Logs”。我认为制作多个If语句不会很好,还有其他方法吗?

    Private Sub connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect.Click
        Dim cleoPath As String
        Dim filePath As String
        filePath = System.IO.Path.Combine(TextBox2.Text, "gta_sa.exe")
        cleoPath = System.IO.Path.Combine(TextBox2.Text, "cleo")

        If File.Exists(filePath) Then
            If Directory.Exists(cleoPath) Then
                MsgBox("Pronasli smo cleo fajl u vasem GTA root folderu. Da se konektujete na server morate ga obrisati.")
            Else
                Dim p() As Process
                p = Process.GetProcessesByName("samp")
                If p.Count > 0 Then
                    MsgBox("Vec imate pokrenut SAMP - ugasite ga.")
                Else
                    Try
                        Dim prozess As Process = Process.GetProcessesByName("samp")(0)
                        prozess.Kill()
                    Catch ex As Exception

                    End Try
                    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\SAMP", "PlayerName", TextBox1.Text)
                    Process.Start("samp://193.192.58.55:7782")

                    Dim client As New TcpClient
                    client.Connect("193.192.58.55", 10924)
                    Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
                    Dim stream As NetworkStream = client.GetStream()
                    stream.Write(sendbytes, 0, sendbytes.Length)
                End If
            End If
        Else
            MsgBox("Da se konektujete morate locirati GTA San Andreas folder.")
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您可以使用与directoryinfo结合的扩展方法来检查是否存在任何目录列表。取决于您是否只需要真/假返回,或者您是否需要处理每个目录(例如,通过添加自定义消息以及阻止应用程序继续运行的文件夹名称。

Public Module DirectoryInfoExt
<System.Runtime.CompilerServices.Extension()>
Public Function AnyExists(DirInfo As IO.DirectoryInfo, ParamArray Directories As String()) As Boolean
    Dim MyPath As String = DirInfo.FullName.TrimEnd("\"c) & "\"
    Return Directories.Any(Function(Dir) IO.Directory.Exists(MyPath & Dir))
End Function

<System.Runtime.CompilerServices.Extension()>
Public Function GetExistingDirectories(DirInfo As IO.DirectoryInfo, ParamArray Directories As String()) As IEnumerable(Of String)
    Dim MyPath As String = DirInfo.FullName.TrimEnd("\"c) & "\"
    Return Directories.Where(Function(Dir) IO.Directory.Exists(MyPath & Dir))
End Function
End Module

布尔返回的示例。

Dim BaseDirectory As String = "C:\Games\GTA"
    Dim GamePath As New DirectoryInfo(BaseDirectory)

    ' Will not give the specific path that exists, only a Boolean if any of theses folder are found
    Dim ModsFound As Boolean = GamePath.AnyExists("Images", "Cleo", "Logs", "SomeFolder\Subfolder")

    If Not ModsFound Then
        'Connect
    Else
        ' Do something
    End If

使用列表返回的示例生成一个客户消息提示,说明找到的特定文件夹的名称。

'This version gives a list instead.
    Dim ModsFoundList As IEnumerable(Of String) = GamePath.GetExistingDirectories("Images", "Cleo", "Logs", "SomeFolder\Subfolder")
    Dim HasMod As Boolean = ModsFoundList.Count > 0

    If HasMod Then
        EvilModdedUserPrompt(ModsFoundList)
        Exit Sub
    End If

    ' Here, since HasMod is false, we can proceed with connection.
    ' Do connection

至于EvilModdedUserPrompt方法

Public Sub EvilModdedUserPrompt(ModsFoundList As List(Of String))
    Dim OutputMessage As New Text.StringBuilder
    OutputMessage.AppendLine("The following mods have been detected on your system:")
    For Each Moditem As String In ModsFoundList
        OutputMessage.AppendFormat("- {0}", Moditem)
        OutputMessage.AppendLine()
    Next
    MessageBox.Show(OutputMessage.ToString)

End Sub