分配 CIM_DATETIME 时 WMI 类型不匹配
WMI Type Mismatch when assigning CIM_DATETIME
我认为我遇到的问题是 CIM_DATETIME 和 DateTime 之间的类型不匹配。
下面是通用服务器中的一个函数 class。在调用此函数时,我已经创建了一个 ManagementScope 对象并通过测试 WMI 查询验证了连接。
public void Sync()
{
ManagementPath path = new ManagementPath("Win32_OperatingSystem=@");
ManagementObject classInstance = new ManagementObject(this.Scope, path, null);
ManagementBaseObject inParams = classInstance.GetMethodParameters("SetDateTime");
inParams["LocalDateTime"] = "20170811101838.000000+000";
ManagementBaseObject outParams = classInstance.InvokeMethod("SetDateTime", inParams, null);
Console.WriteLine(outParams["ReturnValue"]);
}
当我尝试为 "LocalDateTime" 属性 赋值时,我遇到了类型不匹配异常。
如果我尝试获取 inParams["LocalDateTime"]
的类型,它会导致空引用异常。这让我相信 LocalDateTime 不是一个有效的索引,或者它只是引用了一个空对象。
但是,通过枚举其属性,我可以看到 LocalDateTime 是 inParams
的 属性。
foreach (var prop in inParams.Properties)
{
Console.WriteLine($"IsArray : {inParams.IsArray}");
Console.WriteLine($"IsLocal : {inParams.IsLocal}");
Console.WriteLine($"Name : {inParams.Name}");
Console.WriteLine($"Origin : {inParams.Origin}");
Console.WriteLine($"Type : {inParams.Type}");
Console.WriteLine($"Value : {inParams.Value}");
}
这个returns以下,
IsArray : False
IsLocal : False
Name : LocalDateTime
Origin : __PARAMETERS
Type : DateTime
Value :
所以,我的问题是
如何正确引用classWin32_OperatingSystem
的SetDateTime
方法的LocalDateTime
参数?
是否可以转换为CimType.DateTime
?
如果以后有人偶然发现这个...
我需要使用 ManagementDateTimeConverter.ToDmtfDateTime 方法将 System.DateTime 转换为 DMTF 日期时间。
inParams["LocalDateTime"] = ManagementDateTimeConverter.ToDmtfDateTime(DateTime.Now);
反之,从 DMTF 日期时间到 System.DateTime 需要使用 ManagementDateTimeConverter.ToDateTime 方法。
var systemDateTime = ManagementDateTimeConverter.ToDateTime("20170811101838.000000+000");
我认为我遇到的问题是 CIM_DATETIME 和 DateTime 之间的类型不匹配。
下面是通用服务器中的一个函数 class。在调用此函数时,我已经创建了一个 ManagementScope 对象并通过测试 WMI 查询验证了连接。
public void Sync()
{
ManagementPath path = new ManagementPath("Win32_OperatingSystem=@");
ManagementObject classInstance = new ManagementObject(this.Scope, path, null);
ManagementBaseObject inParams = classInstance.GetMethodParameters("SetDateTime");
inParams["LocalDateTime"] = "20170811101838.000000+000";
ManagementBaseObject outParams = classInstance.InvokeMethod("SetDateTime", inParams, null);
Console.WriteLine(outParams["ReturnValue"]);
}
当我尝试为 "LocalDateTime" 属性 赋值时,我遇到了类型不匹配异常。
如果我尝试获取 inParams["LocalDateTime"]
的类型,它会导致空引用异常。这让我相信 LocalDateTime 不是一个有效的索引,或者它只是引用了一个空对象。
但是,通过枚举其属性,我可以看到 LocalDateTime 是 inParams
的 属性。
foreach (var prop in inParams.Properties)
{
Console.WriteLine($"IsArray : {inParams.IsArray}");
Console.WriteLine($"IsLocal : {inParams.IsLocal}");
Console.WriteLine($"Name : {inParams.Name}");
Console.WriteLine($"Origin : {inParams.Origin}");
Console.WriteLine($"Type : {inParams.Type}");
Console.WriteLine($"Value : {inParams.Value}");
}
这个returns以下,
IsArray : False
IsLocal : False
Name : LocalDateTime
Origin : __PARAMETERS
Type : DateTime
Value :
所以,我的问题是
如何正确引用class
Win32_OperatingSystem
的SetDateTime
方法的LocalDateTime
参数?是否可以转换为
CimType.DateTime
?
如果以后有人偶然发现这个...
我需要使用 ManagementDateTimeConverter.ToDmtfDateTime 方法将 System.DateTime 转换为 DMTF 日期时间。
inParams["LocalDateTime"] = ManagementDateTimeConverter.ToDmtfDateTime(DateTime.Now);
反之,从 DMTF 日期时间到 System.DateTime 需要使用 ManagementDateTimeConverter.ToDateTime 方法。
var systemDateTime = ManagementDateTimeConverter.ToDateTime("20170811101838.000000+000");