We help IT Professionals succeed at work.

C#.Net WMI - User credentials cannot be used for local connections

iepaul used Ask the Experts™
My application is reading registry values from multiple computers on the network and can be run from various computers.  It runs fine connecting to remote systems but I get an error when it checks the computer the application is running on.  How can I either detect that the computer is local and connect differently or else alter the WMI query so it treats the local computer that same as all others?

            ConnectionOptions options = new ConnectionOptions();
            options.Impersonation = ImpersonationLevel.Impersonate;
            options.EnablePrivileges = true;
            options.Username = strUser;
            options.Password = strPassword;

            ManagementScope myScope = new ManagementScope("\\\\" + strComputer + "\\root\\default", options);
            ManagementPath mypath = new ManagementPath("StdRegProv");
            ManagementClass mc = new ManagementClass(myScope, mypath, null);

            ManagementBaseObject inParams = mc.GetMethodParameters("GetStringValue");
            inParams["hDefKey"] = hDefKey;
            inParams["sSubKeyName"] = sSubKeyName;
            inParams["sValueName"] = sValueName;

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008
I would compare "strComputer" against Environment.MachineName.


here is what I came up with

        private bool IsLocalIpAddress(string host)
                IPAddress[] hostIPs = Dns.GetHostAddresses(host);
                IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

                foreach (IPAddress hostIP in hostIPs)
                    if (IPAddress.IsLoopback(hostIP)) return true;
                    foreach (IPAddress localIP in localIPs)
                        if (hostIP.Equals(localIP)) return true;
            catch { }
            return false;

Open in new window