函数返回true

时间:2015-12-22 07:07:07

标签: vb.net function return

我试图创建一个函数来搜索在参数中传递的进程,如果存在则返回true但它总是返回false

Public Function ps_running(ByVal name As String) As Boolean 
    For Each Proc As Process In Process.GetProcesses
        If Proc.ProcessName.StartsWith(name) AndAlso (name.Length <> 0)  Then
            Return True
        End If
    Next
    Return False
End Function

我确定它是基本的,我不知道但是我无法说出什么 谢谢

1 个答案:

答案 0 :(得分:0)

检查一下:

Sub Main()
    For Each Proc As Process In Process.GetProcesses
        Console.WriteLine(Proc.ProcessName)
    Next

    Console.WriteLine()

    Dim b As Boolean = ps_running("dwm")
    Console.WriteLine(b)

    Console.ReadLine()
End Sub

Public Function ps_running(ByVal name As String) As Boolean
    If (String.IsNullOrEmpty(name)) Then Return False

    For Each Proc As Process In Process.GetProcesses
        If Proc.ProcessName.ToUpper.StartsWith(name.ToUpper) Then
            Return True
        End If
    Next

    Return False
End Function

使用完整测试编辑