How do I get list of Process Names running, in VB.NET?

前端 未结 4 1518
轮回少年
轮回少年 2020-12-11 03:04

I am trying to find out if an instance of an application (not vb.net) is already running - because I will want to start it but I don\'t want to start it if it is already run

相关标签:
4条回答
  • 2020-12-11 03:48

    I need to get a list of running processes, by name, and test if "processname" is a substring of the name.

    You could use:

    Dim procExists as Boolean = Process.GetProcesses().Any(Function(p) p.Name.Contains(processName))
    

    This will look through all of the processes, and set the procExists value to True if any process which contains processName exists in the currently executing processes. This should handle the existence of the unknown version number as well as the *32 that may occur if you're running on a 64bit OS (that's the WOW64 flag saying that it's a 32bit process running on a 64bit OS).

    0 讨论(0)
  • 2020-12-11 03:48

    another way:

        Dim psList() As Process
        Try
            psList = Process.GetProcesses()
    
            For Each p As Process In psList
                Console.WriteLine(p.Id.ToString() + " " + p.ProcessName)
            Next p
    
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        Console.ReadKey()
    
    0 讨论(0)
  • 2020-12-11 04:00

    You can loop through the running processes like this:

    For Each p As Process In Process.GetProcesses()
       Debug.WriteLine(p.ProcessName)
    Next
    
    0 讨论(0)
  • 2020-12-11 04:01

    Just tried the answer posted of Reed Copesy and this seems to be changed, what worked for me is:

    Dim procExists as Boolean = Process.GetProcesses().Any(Function(p) p.ProcessName.Contains(processName))
    

    Also is possible to retrieve the array by name and check directly if is contained on the process array:

    Process.GetProcessesByName(processName).Length > 0
    

    Thanks!

    0 讨论(0)
提交回复
热议问题