通过WMI的Windows安装日期返回不同的值 [英] Windows Install Date via WMI returns Different Values

查看:153
本文介绍了通过WMI的Windows安装日期返回不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WMI获取Windows安装日期以及其他注册表信息,并且根据我的要求,价值也不同。

不幸的是,我不知道它是怎么回事是不同的 - 我只有CRC,这是不同的。



我最终使用此代码:

 ManagementObjectSearcher searcher =  new  ManagementObjectSearcher(  SELECT *来自Win32_OperatingSystem); 
foreach (ManagementObject wmi_Windows in searcher.Get())
{
尝试
{
s = wmi_Windows [ InstallDate]的ToString();
DateTime dc = ToDateTime(s);
WindowsInfo.InstallDate = dc.AddTicks(-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Ticks)。ToLocalTime()。ToString();
break ;
}
catch (例外情况)
{
// 这里的东西
}
}





假设这段代码在A类中。如果我通过类BI调用包含此代码的方法得到一个结果,但如果我使用类C调用类B来调用类A,结果就不同了。但是,仅适用于某些计算机。当然,办公室里的电脑根本没有这个问题。



有没有人对如何为安装日期获取不同的值有任何想法?其他东西,如Windows序列号返回始终如一。此外,有这个问题的计算机并不都返回相同的值,因此这些值不仅仅是CRC的默认日期。



更新:

我写一个小应用程序在客户端计算机上运行,​​从上面打印出s,dc和WindowsInfo.InstallDate,并在运行中运行systeminfo,并在PowerShell中运行三件事

(Get-WmiObject Win32_OperatingSystem).InstallDate 
([WMI]'')。ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate)
[timezone] :: CurrentTimeZone.ToLocalTime (([datetime]'1/1/1970')。AddSeconds($(get-itemproperty'HKLM:\Software\Microsoft \ Windows NT.\\ CurrentVersion')。InstallDate))



所有内容都返回相同的答案

  20110411090106  .000000 + 120 
11 。 april 2011 09:01:06
11 04 2011 09:01:06



因此问题只发生在应用程序中,当从两个不同的类运行相同的代码时。

解决方案

(get-itemproperty'HKLM:\Software \ Microsoft \ Windows \ Windows \ CurrentVersion') .InstallDate))



所有内容都返回相同的答案

  20110411090106  .000000 +120 
11 。 april 2011 09:01:06
11 04 2011 09:01:06



因此问题只发生在应用程序中,当从两个不同的类运行相同的代码时。


那是因为

(Get-WmiObject Win32_OperatingSystem).InstallDate 

存储为 WMI datetime 数据类型,它真正存储Microsoft UTC格式(yyyymmddHHMMSS.xxxxxx±UUU),其中xxxxxx为毫秒,±UUU是与格林威治标准时间不同的分钟数,因为此 帖子标题为如何获得最准确Windows安装日期(调整时区)完全解释代码。



基本上,您必须在计算中添加时区(TMZ)偏移量(±UUU) 。

 Insta llDate = new DateTime(年,月,日,小时,分钟,秒,msecs)+ UTCoffset; 


I am using WMI to get the windows install date along with other registry information, and depending on how I ask for it, the value is different.
Unfortunately, I don't know how it is different - all I have is the CRC and that is different.

I am ultimately using this code:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (ManagementObject wmi_Windows in searcher.Get())
{                   
  try
  {
    s = wmi_Windows["InstallDate"].ToString();
    DateTime dc = ToDateTime(s);
    WindowsInfo.InstallDate = dc.AddTicks(-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Ticks).ToLocalTime().ToString();
     break;
   }
   catch (Exception ex)
   {
     //stuff here
   }
 }



Lets say this code is in class A. If I call the method containing this code via class B I get one result, but if I use class C to call class B to call class A, the result is different. However, only for some computers. Of course, the computers in the office do not have this problem at all.

Does anyone have any ideas as to how I could get different values for the Install Date? Other things like the windows serial number return consistently the same. Also, computers that have this problem do not all return the same value, so the values is not simply a CRC'd default date.

Update:
I write a wee app to run on the clients computer that prints out s, dc, and WindowsInfo.InstallDate from above, and also ran systeminfo in run, and and ran three things in PowerShell

(Get-WmiObject Win32_OperatingSystem).InstallDate
([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate)
[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($(get-itemproperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').InstallDate))


And everything returned the same answers

20110411090106.000000+120
11. april 2011 09:01:06
11.04.2011 09:01:06


So the problem only occurs in the app, when running the same code from two different classes.

解决方案

(get-itemproperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').InstallDate))


And everything returned the same answers

20110411090106.000000+120
11. april 2011 09:01:06
11.04.2011 09:01:06


So the problem only occurs in the app, when running the same code from two different classes.


That's because

(Get-WmiObject Win32_OperatingSystem).InstallDate

is stored as WMI datetime datatype which really stores a Microsoft UTC format (yyyymmddHHMMSS.xxxxxx±UUU) where crucially xxxxxx is milliseconds and ±UUU is number of minutes different from Greenwich Mean Time as this post entitled "How to get the most accurate Windows Install Date (time zone adjusted)" fully explains with code.

Essentially, you have to add the time zone (TMZ) offset (±UUU) to your calculations.

InstallDate = new DateTime(year, month, date, hour, mins, secs, msecs) + UTCoffset; 


这篇关于通过WMI的Windows安装日期返回不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆