标题: [问题求助] VBS的SWbemObjectPath.IsSingleton属性怎么理解?[已解决] [打印本页]
作者: czjt1234 时间: 2013-3-10 09:43 标题: VBS的SWbemObjectPath.IsSingleton属性怎么理解?[已解决]
本帖最后由 czjt1234 于 2013-3-11 09:17 编辑
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394239(v=vs.85).aspx
这里指明Win32_OperatingSystem is a singleton class. To get the single instance, use "@" for the key- Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
- Set objSWbemServices = objSWbemLocator.ConnectServer()
- Set objSWbemObject = objSWbemServices.Get("Win32_OperatingSystem")
- Msgbox objSWbemObject.Path_.IsSingleton
- Set objSWbemObjectSet = objSWbemServices.InstancesOf("Win32_OperatingSystem")
- For Each objSWbemObject In objSWbemObjectSet
- Msgbox objSWbemObject.Path_.IsSingleton
- Next
复制代码
怎么都显示False?
作者: czjt1234 时间: 2013-3-10 10:02
我又构建了一个代码验证该属性,设置一个新的类的该属性为True
但是该类还是能设置2个实例
难道这个属性不是标志该类只有一个实例?
- Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
- Set objSWbemServices = objSWbemLocator.ConnectServer()
- Set objSWbemObject = objSWbemServices.Get() '获取一个空类的实例
- objSWbemObject.Path_.Class = "NewClass" '设置类名
- objSWbemObject.Properties_.Add "Property01", 8 '为这个类添加一个字符串属性
- objSWbemObject.Properties_("Property01").Qualifiers_.Add "key", True '设定一个主键属性
- Set objSWbemObjectPath = objSWbemObject.Put_() '更新该类到WMI中,同时返回一个SWbemObjectPath对象
- Msgbox "IsSingleton:" & objSWbemObjectPath.IsSingleton 'False
- objSWbemObjectPath.SetAsSingleton()
- Msgbox "IsSingleton:" & objSWbemObjectPath.IsSingleton 'True
- Set objSWbemObject = objSWbemServices.Get("NewClass")
- Set objNewInst = objSWbemObject.SpawnInstance_() '为这个类增加一个实例
- objNewInst.Property01 = "New00" '设置实例的属性值
- Set objNewInst1 = objSWbemObject.SpawnInstance_() '为这个类再增加一个实例
- objNewInst1.Property01 = "New01" '设置实例的属性值
- objNewInst.Put_() '更新该类到WMI中
- objNewInst1.Put_()
- Set objSWbemObjectSet = objSWbemServices.InstancesOf("NewClass")
- Msgbox "这个类有 " & objSWbemObjectSet.Count & " 个实例"
- objSWbemObject.Delete_() '删除这个类
复制代码
作者: Demon 时间: 2013-3-10 22:39
楼主这次打算花多少个小时弄明白?
作者: czjt1234 时间: 2013-3-11 07:15
本帖最后由 czjt1234 于 2013-3-11 07:16 编辑
SetAsClass
SetAsSingleton
在msdn和百度和google里都搜了,没有这2个方法的代码示例
只能自己猜
所以IsSingleton的意思也凭感觉去凭空捏造
作者: czjt1234 时间: 2013-3-11 09:17
搞定,简单的示例一下- strPath = "\\a\b\c\d:e.f='g'"
- 'strPath = "IIsWebServer='W3SVC/1180970907'" '尝试更换字符串查看变化
-
- Set objSWbemObjectPath = CreateObject("WbemScripting.SWbemObjectPath")
- objSWbemObjectPath.Path = strPath
-
- 'objSWbemObjectPath.SetAsClass() '去掉行首的'查看变化,按类名的标准,格式化字符串
- 'objSWbemObjectPath.SetAsSingleton() '去掉行首的'查看变化,按实例名=@的标准,格式化字符串
-
- Msgbox "Authority " & vbTab & objSWbemObjectPath.Authority & vbCrLf & _
- "Class " & vbTab & objSWbemObjectPath.Class & vbCrLf & _
- "IsClass " & vbTab & objSWbemObjectPath.IsClass & vbCrLf & _
- "IsSingleton " & vbTab & objSWbemObjectPath.IsSingleton & vbCrLf & _
- "Locale " & vbTab & objSWbemObjectPath.Locale & vbCrLf & _
- "Namespace " & vbTab & objSWbemObjectPath.Namespace & vbCrLf & _
- "ParentNamespace" & vbTab & objSWbemObjectPath.ParentNamespace & vbCrLf & _
- "Path " & vbTab & objSWbemObjectPath.Path & vbCrLf & _
- "Relpath " & vbTab & objSWbemObjectPath.Relpath & vbCrLf & _
- "Server " & vbTab & objSWbemObjectPath.Server & vbCrLf & _
- "DisplayName " & vbTab & objSWbemObjectPath.DisplayName
-
- Set objSWbemNamedValueSet = objSWbemObjectPath.Keys
- For Each objSWbemNamedValue In objSWbemNamedValueSet
- Msgbox "Name:" & objSWbemNamedValue.Name &vbCrLf & _
- "Value:" & objSWbemNamedValue.Value
- Next
-
- 'objSWbemNamedValue.Name 表示.之后=之前的字符串
- 'objSWbemNamedValue.Value 表示''之间的字符串
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |