vb.net:
Private Function IsRunning(ByVal projname As String) As Boolean
Dim CurrentProcess As Process = Process.GetCurrentProcess()
Dim processes As Process() = Process.GetProcessesByName("AAA")
Dim p As Process
For Each p In processes
If p.Id <> CurrentProcess.Id Then
If CurrentProcess.MainModule.FileName = Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\") Then
'MsgBox("系统已打开", MsgBoxStyle.Information, "提示")
'Me.Close()
Return True
End If
End If
Next
Return False
End Function
c#:
private Boolean IsRunning(string projname)
{
Process CurrentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(projname);
//Process p;
foreach(Process p in processes)
{
if (p.Id != CurrentProcess.Id )
{
if (CurrentProcess.MainModule.FileName == System.Reflection.Assembly.GetExecutingAssembly().Location.Replace('/','\\'))
{
return true;
}
}
}
return false;
}
注:该方法需要放置在需要执行的程序中,先执行该函数,如果返回True则退出程序。
本文提供了两种方法,使用VB.NET和C#检查指定进程(如'AAA')是否正在运行。通过获取当前进程并遍历所有进程,比较进程ID和主要模块文件路径来判断。如果找到匹配的进程,函数将返回True,否则返回False。这种方法适用于在程序启动时自我检测并根据运行状态决定是否退出。
767

被折叠的 条评论
为什么被折叠?



