返回列表 发帖
试试这样:
'======================================================================
'程序开始------检测迅雷是否运行-----是-----开始网络监视
'                      |                        ↑
'                      否                         |
'                      |                        |
'         ↑------ 一直监视系统进程-----迅雷运行---
'         |              |
'         ----------- 未运行
'======================================================================
On Error Resume Next
Public CurrentFlowInt, ProFlag
ConnectionID = "本地连接"
Set ObjectNetwork = CreateObject("Wscript.Network")
strComputer = ObjectNetwork.ComputerName
'获取计算机名,必须用字符串,不能用.代替
Set ObjectWSH = Wscript.createObject("Wscript.shell")
Set ObjWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set NetCards = ObjWMIService.ExecQuery("Select * from Win32_NetworkAdapter WHERE NetConnectionID='" & ConnectionID & "'")
If NetCards.count<>0 Then
        For Each NetCard In NetCards
                WScript.Echo "Name: " & NetCard.Name & "        MACAddress: " & NetCard.MACAddress
                NetCardName = NetCard.Name
                strNet = Split(NetCard.Name, " ", -1, 1)
                str1 = strNet(0) & strNet(1) '& strNet(2)
        Next
        Else
        Wscript.Echo "网卡不存在."
        WScript.Quit(0)
End If
'获取用于上网的网卡信息
Set CheckProcess = ObjWMIService.ExecQuery("Select * from Win32_Process WHERE Name='Thunder.exe'")
If CheckProcess.count<>0 Then
        ProFlag = 0
        Else
        ProFlag = 1
End If
Set colMonitoredProcesses = objWMIService.ExecNotificationQuery("select * from __instancecreationevent " & " within 1 where TargetInstance isa 'Win32_Process'")
i = 0
Do While i = 0
    Set CheckProcess = ObjWMIService.ExecQuery("Select * from Win32_Process WHERE Name='Thunder.exe'")
        If CheckProcess.count<>0 Then
                Wscript.Echo "迅雷已运行."
                '监视网络
                CurrentFlow
                BeginFlow = CurrentFlowInt
                '获取开始时接收的流量(字节)
                Wscript.Echo "BeginFlow: " & BeginFlow
                WScript.Sleep 120000
                '定义时间范围
        Else
                Wscript.Echo "迅雷未运行."
                Set objLatestProcess = colMonitoredProcesses.NextEvent
                '监视迅雷
            If objLatestProcess.TargetInstance.Name = "Thunder.exe" Then
                        Wscript.Echo "迅雷已运行."
                        CurrentFlow
                        BeginFlow = CurrentFlowInt
                        '获取开始时接收的流量(字节)
                        Wscript.Echo "BeginFlow: " & BeginFlow
                        WScript.Sleep 120000
                        '定义时间范围
                End If
        End If
        
        CurrentFlow
        EndFlow = CurrentFlowInt
        '获取结束时接收的流量(字节)
        Wscript.Echo "EndFlow: " & EndFlow
        Result = EndFlow - BeginFlow
        If Result<20480 Then
                '10240即10KB,当120000毫秒(120秒=2分钟)(上面的时间范围)内接收流量小于20KB就关闭系统
                WScript.Echo "关闭系统."
                ObjectWSH.Run"shutdown.exe -s -t 0", 0, TRUE
        End If
        '计算结果并比较
        WScript.Echo "Exit ..."
        WScript.Quit(0)
Loop
Function CurrentFlow
        Set ObjectFlows = objWMIService.InstancesOf("Win32_PerfRawData_Tcpip_NetworkInterface")
        For Each ObjectFlow In ObjectFlows
                strNetName = Split(ObjectFlow.Name, " ", -1, 1)
                str2 = strNetName(0) & strNetName(1) '& strNetName(2)
                If str1 = str2 Then
                        CurrentFlowInt = ObjectFlow.BytesReceivedPersec
                End If
        Next
End FunctionCOPY
1

评分人数

    • CrLf: 乐于助人技术 + 1
---学无止境---

TOP

回复 37# ygqiang


    36楼代码试试看?
---学无止境---

TOP

36楼代码,我这里是测试成功了。

你那没成功有没有提示什么?比如  这个提示BeginFlow:  后面有没有数字?
---学无止境---

TOP

返回列表