# -*- coding:utf-8 -*-
import datetime
import os
import wmi
import time
import _winreg
import pythoncom
import threading
import win32api
import win32con
import Queue
c = wmi.WMI()
# 如果要连接远程机器,只需要在WMI构造器中指定远程机器名即可
# c = wmi.WMI("some_other_machine")
# List All Running Processes
# 列出所有正在运行的进程
for process in c.Win32_Process():
print process.ProcessID,process.Name
# List All Running Notepad Processes
# 列出所有正在运行的记事本进程
for process in c.Win32_Process(name="notepad.exe"):
print process.ProcessID,process.Name
# Create And Then Destroy A New Notepad Process
# 创建一个新的记事本进程然后结束它
process_id, return_value = c.Win32_Process.Create(CommandLine="notepad.exe")
for process in c.Win32_Process(ProcessId=process_id):
print process.ProcessID, process.Name
result = process.Terminate()
# Show The Interface For The .Create Method Of A Win32_Process Class
# 显示Win32_Process类的.Create方法的接口
# 注:wmi模块会接受WMI方法的传入参数作为Python的关键字参数,并把传出参数作为一个元组进行返回。
print c.Win32_Process.Create
# Show All Automatic Servieces Which Are Not Running
# 显示没有处于正常运行状态的自启动服务
stopped_services = c.Win32_Service(StartMode="Auto", State="Stopped")
if stopped_services:
for s in stopped_services:
print s.Caption, "service is not running"
else:
print "No auto service stopped"
# Show The Percentage Free Space For Each Fixed Disk
# 显示每个固定磁盘的剩余空间百分比
for disk in c.Win32_LogicalDisk(DriveType=3):
print disk.Caption, "%0.2f%% free" % (100.0 * long(disk.FreeSpace) / long(disk.Size) )
# Run Notepad, Wait Until It's Closed And Then Show Its Text
# 运行记事本,等它关闭之后显示它里面的文字
# 注:这个例子是运行一个进程并且知道它什么时候结束,而不是去处理输入到记事本里面的文字。\
# 所以我们只是简单的用记事本打开一个指定文件,等到用户完成输入并关闭记事本之后,显示一下它的内容。
filename = r'E:\Tools\test.txt'
process = c.Win32_Process
process_id, result = process.Create(CommandLine="notepad.exe " + filename)
watcher = c.watch_for(
notification_type = "Deletion",
wmi_class = "Win32_Process",
delay_secs = 1,
<