How to check if process is open and if not, open it?

Asked

Viewed 40 times

1

Hello, I am learning VB.net and would like help. I’m doing a little program to check if a process is open and if it’s not, open it.

I’ve come this far:

    Private Sub CheckIfRunning()
    p = Process.GetProcessesByName(TextBox1.Text)
    If p.Length < 1 Then
        Process.Start(TextBox1.Text)
    End If
End Sub

The problem is that it keeps opening the process several times, even if it is already open. Someone can help me with this?

  • What is the content of TextBox1.Text?

1 answer

0


Hello, Sergio!

Try it this way. Note: I don’t know how you are passing the name of the process, but Getprocessesbyname has to receive the name of the process without extension already Process.Start() needs the extension.

Private Sub CheckIfRunning()

    Dim p As Process = System.Diagnostics.Process.GetProcessesByName(Path.GetFileNameWithoutExtension(TextBox1.Text))

    If p.Length < 1 Then
        Process.Start(TextBox1.Text)
    End If
End Sub
  • Hi Matheus, man it worked! Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.