我正在尝试禁用本地
Windows帐户.借助Microsoft librairy,我能够写出这个
ManagementScope mgmtScope = new ManagementScope("\\\\.\\Root\\CIMv2");
ObjectQuery oQuery = new ObjectQuery("SELECT * FROM Win32_UserAccount");
ManagementObjectSearcher mgmtSearch = new ManagementObjectSearcher(mgmtScope, oQuery);
ManagementObjectCollection objCollection = mgmtSearch.Get();
foreach (ManagementObject mgmtObject in objCollection)
{
if ((string)mgmtObject["Name"] == "test")
{
mgmtObject.SetPropertyValue("Disabled", true);
Console.WriteLine("{0}", mgmtObject.GetPropertyValue("Disabled"));
}
}
GetPropertyValue函数告诉我该值已设置为“true”,但是当程序退出循环foreach时,该值再次设置为“false”.
有谁知道为什么会这样?
编辑:我最终找到了答案.我只需要使用方法ManagementObject.Put()将更改应用于对象
最佳答案 使用该方法将更改应用于对象
ManagementObject.Put()