locked
Accessing System.Management.ManagementObjectSearcher by non-admin user throws "Access Denied" exception RRS feed

  • Question

  • I'm creating the object as follows:

    ManagementObjectSearce obj

    =

    new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");

    and getting "Access Denied" exception when run by non-administrator user, even if the user belongs to Administrators group. The code works fine when logged in as Administrator. The code queries serial ports on local machine. Thanks in advance!


    PT
    Monday, August 15, 2011 9:02 PM

Answers

All replies

  • First try other query like "Select * from Win32_OperatingSystem" (or Win32_Process whatever)

    1. If you still get access denied, the probable the issue is with use account (I believe you are not autherized to execute the WMI queries)

    Or

    2. If you are able to execute the query, then probable you are not allowed to deal with serial port (this is highly unlikely though).


    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, August 16, 2011 6:16 AM
  • Assuming your OS is Windows Vista or Windows 7 or Windows 2008, you are experiencing a "UAC" issue. You could confirm this by disabling UAC and then running your App.
    Wednesday, August 17, 2011 7:07 AM
  • I can do SELECT * FROM Win32_SerialPort with no problems, for example, but it lists only physical ports. It looks like the problem here not MSSerial_PortName, but root\WMI management scope.
    PT
    Wednesday, August 17, 2011 2:43 PM
  • You are absolutely correct! The problem is - I can't afford to disable UAC. I also tried authenticating in code but it didn't work for me, don't know why yet.
    PT
    Wednesday, August 17, 2011 2:48 PM
  • One of the ways to selectively disable UAC for your application is to use the "Application Compatability Toolkit". More information can be found at http://www.techrepublic.com/blog/window-on-windows/selectively-disable-uac-for-your-trusted-vista-applications/635. Hope it works for you.
    • Marked as answer by Paul Zhou Wednesday, August 24, 2011 6:16 AM
    Thursday, August 18, 2011 5:15 AM
  •  

    Disable UAC

    There are several ways to do that, I suggest two ways for you:

    1.      Modify the registry to disable UAC.

    You can set the disableUAC  Value in the registry (HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System)  to be 0 to disable and to 1 to enable.

    2.      Disable UAC in manifest.

    You can follow the solution from this thread.

     

    Much further information:

    UAC: All Information Developers need about the User Account Control (UAC)

    /MANIFESTUAC

    How to embed a manifest in an assembly


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Wednesday, August 24, 2011 6:16 AM
    Thursday, August 18, 2011 8:14 AM