任何人都有示例代码,可以对CIM_DataFile类进行WMI查询吗?

时间:2009-06-02 23:04:10

标签: .net wmi

用SELECT来获取有关文件或目录的信息?什么东西真的有效?

语言没关系,我对查询语法最感兴趣。

3 个答案:

答案 0 :(得分:3)

以下是一些例子:

  • 查询文件:

    SELECT * FROM CIM_DataFile WHERE Name='C:\\WINDOWS\\NOTEPAD.EXE'
    SELECT * FROM CIM_DataFile WHERE Drive='C:' AND Path='\\Windows\\' AND FileName='Notepad' AND Extension='EXE'
    
  • 查询文件夹:

    SELECT * FROM CIM_Directory WHERE Name='C:\\Windows'
    SELECT * FROM CIM_Directory WHERE Drive='C:' AND Path='\\Program Files\\' AND FileName='Internet Explorer'
    

有关查询语法的详细信息,请参阅WQL (SQL for WMI)。另请参阅CIM_DataFileCIM_Directory,以获取可在SELECTWHERE子句中使用的文件和文件夹属性列表。

答案 1 :(得分:1)

WMI Code Creator有一些帮助 看到这段代码:

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM CIM_DataFile",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "CIM_DataFile instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "AccessMask: " & objItem.AccessMask
    Wscript.Echo "Archive: " & objItem.Archive
    Wscript.Echo "LastAccessed: " & objItem.LastAccessed
    Wscript.Echo "LastModified: " & objItem.LastModified
    Wscript.Echo "Manufacturer: " & objItem.Manufacturer
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "Path: " & objItem.Path
    Wscript.Echo "Readable: " & objItem.Readable
    Wscript.Echo "Status: " & objItem.Status
    Wscript.Echo "System: " & objItem.System
    Wscript.Echo "Version: " & objItem.Version
    Wscript.Echo "Writeable: " & objItem.Writeable
Next

修改

此示例是您正在查看Enumerating All the Files on a Computer

请尝试以下格式查询:

Select * From Win32_Directory Where FileName = 'Scripts'

以下是有关make query WQL using LIKE的更多信息 也请尝试这样做:Scriptomatic 2.0

答案 2 :(得分:1)

我已经达成了答案,而这一切都是由我的一个愚蠢错误引起的:我没有将路径字符串中的反斜杠加倍,并认为它已经完成了。此外,您必须在WHERE子句中指定所有四个字段:驱动器,路径,文件名和扩展名。