杀死最后在任务pipe理器vb.net中创build的Excel实例

你能帮我解决我的问题吗?

我有几个Excel实例打开(任务pipe理器EXCEL.EXE),我想杀死最后打开的实例。 现在我可以杀死一审,但我想杀死最后一个:

Sub Main() Dim dictExe As Object dictExe = CreateObject("Scripting.Dictionary") Dim oServ As Object Dim cProc Dim oProc As Object oServ = GetObject("winmgmts:") cProc = oServ.ExecQuery("Select * from Win32_Process") For Each oProc In cProc If oProc.Name = "EXCEL.EXE" Then dictExe.Add(oProc, oProc.CreationDate) End If Next For Each oProcCatia In dictExe If oProcCatia.Name = "EXCEL.EXE" Then Dim errReturnCode errReturnCode = oProcCatia.Terminate() Exit Sub End If Next End Sub 

我想杀字典女巫有最大的创builddate,但我不知道如何。 所以问题也可以,如何从字典中获得最大价值的关键?

我会放弃使用Scripting.Dictionary,只是保留最新的CreationDate的引用:

 Sub Main() Dim oServ As Object Dim cProc Dim oProc As Object Dim oToBeRemovedProc As Object Dim oToBeRemovedCreationDate As Object oServ = GetObject("winmgmts:") cProc = oServ.ExecQuery("Select * from Win32_Process") For Each oProc In cProc If oProc.Name = "EXCEL.EXE" Then '::: No good checking values against Nothing If oToBeRemovedProc Is Nothing Then oToBeRemovedProc = oProc oToBeRemovedCreationDate = oProc.CreationDate End If '::: Is the current hold value older than this one? If oToBeRemovedCreationDate < oProc.CreationDate Then oToBeRemovedProc = oProc oToBeRemovedCreationDate = oProc.CreationDate End If End If Next '::: Do we actually have something to remove? If Not oToBeRemovedProc Is Nothing Then oToBeRemovedProc.Terminate() oToBeRemovedProc = Nothing oToBeRemovedCreationDate = Nothing End If End Sub