locked
System.Management (WMI) Unauthorized Access error RRS feed

  • Question

  • Can anyone tell me why I would receive an Unauthorized Access Exception for the following simple routine?

    public void GetPrintersCollection() {
      string searchQuery = "SELECT * FROM Win32_Printer";
      string line;
      try {
        using (ManagementObjectSearcher query = new ManagementObjectSearcher(searchQuery)) {
          ManagementObjectCollection Printers = query.Get();
          if (0 < Printers.Count) { // <= Exception HERE!
            ManagementObjectCollection.ManagementObjectEnumerator enumerator = Printers.GetEnumerator();
            enumerator.MoveNext();
            Console.WriteLine(enumerator.ToString());
          }
        }
      } catch (UnauthorizedAccessException err) {
        line = string.Format("PrinterSpool - GetPrintersCollection: You do not have authorization to access this printer.\r\n{0}", err.ToString());
        Console.WriteLine(line);
      } catch (Exception err) {
        line = string.Format("PrinterSpool - GetPrintersCollection: {0}", err.Message);
        Console.WriteLine(line);
      }
    }
    I tried to get around this exception by setting it to a Full Trust application under the ClickOnce Security Settings, but the error is still here.

    My actual error out to the Console is:

    "PrinterSpool - GetPrintersCollection: You do not have authorization to access this printer.\r\nSystem.UnauthorizedAccessException\r\n   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)\r\n   at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()\r\n   at System.Management.ManagementObjectCollection.get_Count()\r\n   at win32_printer.Program.GetPrintersCollection() in C:\\Shared\\jp2Testing\\win32_printer\\Program.cs:line 24"

    Does anyone have a clue how to stop this?


    Avoid Sears Home Improvement
    • Moved by nobugz Monday, October 5, 2009 7:08 PM (From:.NET Base Class Library)
    Monday, October 5, 2009 6:33 PM

Answers

  • PrinterSpool - GetPrintersCollection: You do not have authorization to access this printer

    To reolve this, ask the computer's administrator to give you permission.

    You're kidding, right? How did you get to be an MVP with answers like that?

    The error message you quoted is what I display on the screen in my software. Obviously, I can go to printers and faxes on my machine and access all of my printers - it is the Management Object Searcher that can not.

    We are thinking now that this is a Windows Service issue, as in one of the WMI Services is stopped. I should probably just close this thread.

    Regards,
    ~Joe
    Avoid Sears Home Improvement
    • Marked as answer by jp2code Wednesday, October 7, 2009 8:38 PM
    Wednesday, October 7, 2009 8:38 PM

All replies

  • PrinterSpool - GetPrintersCollection: You do not have authorization to access this printer

    To reolve this, ask the computer's administrator to give you permission.


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Wednesday, October 7, 2009 7:44 PM
  • PrinterSpool - GetPrintersCollection: You do not have authorization to access this printer

    To reolve this, ask the computer's administrator to give you permission.

    You're kidding, right? How did you get to be an MVP with answers like that?

    The error message you quoted is what I display on the screen in my software. Obviously, I can go to printers and faxes on my machine and access all of my printers - it is the Management Object Searcher that can not.

    We are thinking now that this is a Windows Service issue, as in one of the WMI Services is stopped. I should probably just close this thread.

    Regards,
    ~Joe
    Avoid Sears Home Improvement
    • Marked as answer by jp2code Wednesday, October 7, 2009 8:38 PM
    Wednesday, October 7, 2009 8:38 PM
  • I am just stating the obvious in case you missed it. The WMI service is probably using a less privileged user as running credential. Whatever you can do in Windows Explorer does not mean the WMI service user can do the same.

    you can change the running credential of the WMI service in control panel, or rewrite your application to call pinter APIs directly in a desktop process, that would take your current credential.




    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Wednesday, October 7, 2009 8:50 PM