首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >阻止更多文件夹和应用程序

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

Stack Overflow用户
提问于 2016-05-29 01:04:54
回答 1查看 31关注 0票数 0

正如你在这里看到的,我阻塞了名为"cleo“的目录。如果我把它放在我的文件夹里,我不能点击连接。例如,我如何检查浏览文件中是否存在"Cleo“、"Images”、"Logs“。我不认为使用多个If语句会很好,还有其他方法吗?

代码语言:javascript
复制
    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
EN

回答 1

Stack Overflow用户

发布于 2016-05-29 03:18:52

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

代码语言:javascript
复制
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

一个布尔型返回值的例子。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
'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方法

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37501687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档