1
我試圖通過填充ManagementScope變量並嘗試連接到遠程計算機來通過服務連接到Windows WMI。如果我作爲Windows控制檯運行,則連接成功,但在從Windows服務運行相同代碼時失敗。ManagementScope.Connect失敗,並從Windows服務運行時出錯E_ACCESSDENIED錯誤
代碼蔭使用如下:
ManagementScope scope = null;
scope = new ManagementScope("\\\\" + m_sComputerName + "\\root\\cimv2");
if (m_sLoginName != null && m_sPassword != null)
{
scope.Options.Username = m_sLoginName;
scope.Options.Password = m_sPassword;
}
scope.Options.EnablePrivileges = true;
scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Connect();
我運行Windows服務作爲本地系統。代碼是用C#編寫的.net 4.0版本
任何幫助非常感謝。
感謝