判断程序是否已经运行
近段时间,需要写一个小功能,就是需要判断程序是否已经运行。
某个程序安装后,也许被多个用户运行。那怎样判断当前用户已经运行了此程序了呢?
下面是Insus.NET的做法,就是:
《VB.NET WinForm获取运行程序用户名》http://www.cnblogs.com/insus/p/5194839.html
然后:
《WinForm(VB.NET)获取当前登录的用户名》http://www.cnblogs.com/insus/p/5195126.html
从上面的方法,如果二者的值相同,那此程序已经被当前用户运行。
源代码:
Public Shared Function IsRunning(processName As String) As Boolean Dim btnIsRun As Boolean = False Dim owner As String = GetProcessOwner(App) Dim po As String = owner.Substring(owner.LastIndexOf("\") + 1) Try Dim listProc() As System.Diagnostics.Process listProc = System.Diagnostics.Process.GetProcessesByName(processName) If listProc.Length > 0 AndAlso GetUserName() = po Then btnIsRun = True Else btnIsRun = False End If Catch ex As Exception btnIsRun = False End Try Return btnIsRun End Function