我正在尝试禁用本地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()