0

I would like to write a piece of code that will check if a program is running and if it isn't then a MsgBox will appear. I have done this before with a specific file using the code below:

If (Not System.IO.Directory.Exists("\\twa-file2\staffhomes\" + TextBox1.Text)) Then
    MsgBox("Home Area Not Found", MsgBoxStyle.Information, "Error")
End If

Would someone be able to tell me how to do this for a program?

Knowledge Cube
  • 990
  • 12
  • 35
Kyle Snelling
  • 13
  • 1
  • 6
  • 2
    I think your mixing what you want to do. Either you want to check if a file exists (what your doing in your code) or you want to check if a process is running. – Mederic Jun 06 '17 at 14:11
  • If you're checking if a program is running (as opposed to simply looking to see if a directory exists, as your posted code indicates), then https://stackoverflow.com/questions/4722198/checking-if-my-windows-application-is-running may be a helpful starting point. Then you can simply modify your condition accordingly. – Knowledge Cube Jun 06 '17 at 14:23
  • 2
    Side note; use `MessageBox` instead of `MsgBox`. You should look at Googling how to check if a process is running. – Bugs Jun 06 '17 at 14:29
  • medric - I would to know how to do it for a process. The directory code was just an example of what ive done with a directory. I would like to do the same but with a process. – Kyle Snelling Jun 06 '17 at 14:48
  • Chris - that in c# and unfortunately I'm not clever enough to know what it means I have tried a code converter but to know avail. – Kyle Snelling Jun 06 '17 at 14:48
  • @KyleSnelling True, it's for C#, but the key part is [`Process.GetProcesses()`](https://msdn.microsoft.com/en-us/library/1f3ys1f9(v=vs.110).aspx), which can be used in any .NET language (including VB.NET). This method is already suggested in an answer below, which includes a link to MSDN where code samples are provided in C#, C++ and VB.NET. – Knowledge Cube Jun 06 '17 at 15:51
  • I now have the following code: localByName As Process() = Process.GetProcessesByName("pc-client.exe") how would I then make it so a message box appears if it does not find the process? – Kyle Snelling Jun 07 '17 at 13:17

1 Answers1

2

You want to look at the Process.GetProcesses Method ().

Bugs
  • 4,491
  • 9
  • 32
  • 41
apc
  • 5,306
  • 1
  • 17
  • 26
  • i now have the following code: localByName As Process() = Process.GetProcessesByName("pc-client.exe") how would I then make it so a message box appears if it does not find the process? – Kyle Snelling Jun 07 '17 at 13:10
  • You would need to check the length of the array returned. If the length is 0 then the process is not running. – apc Jun 07 '17 at 14:09
  • what's an array and how do I go about doing that? I really an very basic with VB and just trying to find my feet. – Kyle Snelling Jun 08 '17 at 11:22
  • I suggest you take a look at the tutorials/documenation: https://stackoverflow.com/documentation/vb.net/topics however if you are unfamiliar with the concept of an array you may want to consider some programming basics – apc Jun 08 '17 at 12:47