从 LastBootUpTime 返回奇怪的字符串
Weird String returned from LastBootUpTime
我正在 return 使用 System.Management
从 Win32_OperatingSystem
获取值(我知道 Microsoft.Management.Infrastructure
但我只是在做一些测试)。
这就是我运行:
string bootTime = "";
//Creating a management searcher object
ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT LastBootUpTime FROM Win32_OperatingSystem");
//Collection to hold "results"
ManagementObjectCollection objCollection = mgmtObjSearcher.Get();
foreach (ManagementObject mgmtObject in objCollection)
{
bootTime = mgmtObject["LastBootUpTime"].ToString();
}
但是在尝试 运行 Convert.ToDateTime(bootTime).ToString("dd/MM/yyyy hh:mm:ss");
时出现以下错误:
System.FormatException: 'String was not recognized as a valid
DateTime.'
看起来 LastBootUpTime 值被 return 编辑为 20190703085750.500000+060
,我似乎无法使用 Convert.ToDateTime
进行转换并且无法使用 DateTime.Parse
有人可以伸出援助之手,让我知道哪里出错了吗?我只想 return LastBootUpTime 并转换特定的字符串格式 ("dd/MM/yyyy hh:mm:ss")。非常感谢任何帮助:)
根据 Ňɏssa Pøngjǣrdenlarp 和 Soner Gönül 的评论,这就是我的结论:
string bootTime = "";
//Creating a management searcher object
ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT LastBootUpTime FROM Win32_OperatingSystem");
//Collection to hold "results"
ManagementObjectCollection objCollection = mgmtObjSearcher.Get();
foreach (ManagementObject mgmtObject in objCollection)
{
bootTime = ManagementDateTimeConverter.ToDateTime(mgmtObject["LastBootUpTime"].ToString()).ToString("dd/MM/yyyy HH:mm:ss");
}
更新
在 infinit_e 评论之后,这是我在不使用 foreach
循环的情况下获取值的方式:
string bootTime = new ManagementObjectSearcher("SELECT LastBootUpTime FROM Win32_OperatingSystem").Get()
.OfType<ManagementObject>()
.First()
.Properties["LastBootUpTime"].Value.ToString();
我正在 return 使用 System.Management
从 Win32_OperatingSystem
获取值(我知道 Microsoft.Management.Infrastructure
但我只是在做一些测试)。
这就是我运行:
string bootTime = "";
//Creating a management searcher object
ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT LastBootUpTime FROM Win32_OperatingSystem");
//Collection to hold "results"
ManagementObjectCollection objCollection = mgmtObjSearcher.Get();
foreach (ManagementObject mgmtObject in objCollection)
{
bootTime = mgmtObject["LastBootUpTime"].ToString();
}
但是在尝试 运行 Convert.ToDateTime(bootTime).ToString("dd/MM/yyyy hh:mm:ss");
时出现以下错误:
System.FormatException: 'String was not recognized as a valid DateTime.'
看起来 LastBootUpTime 值被 return 编辑为 20190703085750.500000+060
,我似乎无法使用 Convert.ToDateTime
进行转换并且无法使用 DateTime.Parse
有人可以伸出援助之手,让我知道哪里出错了吗?我只想 return LastBootUpTime 并转换特定的字符串格式 ("dd/MM/yyyy hh:mm:ss")。非常感谢任何帮助:)
根据 Ňɏssa Pøngjǣrdenlarp 和 Soner Gönül 的评论,这就是我的结论:
string bootTime = "";
//Creating a management searcher object
ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT LastBootUpTime FROM Win32_OperatingSystem");
//Collection to hold "results"
ManagementObjectCollection objCollection = mgmtObjSearcher.Get();
foreach (ManagementObject mgmtObject in objCollection)
{
bootTime = ManagementDateTimeConverter.ToDateTime(mgmtObject["LastBootUpTime"].ToString()).ToString("dd/MM/yyyy HH:mm:ss");
}
更新
在 infinit_e 评论之后,这是我在不使用 foreach
循环的情况下获取值的方式:
string bootTime = new ManagementObjectSearcher("SELECT LastBootUpTime FROM Win32_OperatingSystem").Get()
.OfType<ManagementObject>()
.First()
.Properties["LastBootUpTime"].Value.ToString();