Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 392629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:10:41+00:00 2026-05-12T16:10:41+00:00

First, this code worked in VS2005. The code in VS2008 crashes with an UnauthorizedAccessException

  • 0

First, this code worked in VS2005.

The code in VS2008 crashes with an UnauthorizedAccessException whenever I try stepping over the foreach loop and assign a ManagementObject.

public static List<string> GetPrintersCollection() {
  if (printers == null) {
    printers = new List<string>();
    string searchQuery = "SELECT * FROM Win32_Printer";
    try {
      using (ManagementObjectSearcher searchPrinters = new ManagementObjectSearcher(searchQuery)) {
        ManagementObjectCollection Printers = searchPrinters.Get(); // <= Printers data below
        foreach (ManagementObject printer in Printers) { // <= Error Here
          printers.Add(printer.Properties["Name"].Value.ToString());
        }
      }
    } catch (UnauthorizedAccessException err) {
      Console.WriteLine(err.Message); // the message is an empty string
      throw new Exception("PrinterSpool - GetPrintersCollection: You do not have authorization to access this printer.");
    } catch (Exception err) {
      throw new Exception(string.Format("PrinterSpool - GetPrintersCollection: {0}", err.Message));
    }
  }
  return printers;
}

StackTrace =
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
at AcpClasses.PrinterSpool.GetPrintersCollection()…

When I try to view the data in the Locals window, these fields look immediately suspect:
1. Count – Value: “Function evaluation timed out.”
2. IsSynchronized – Value: Function evaluation disabled because a previous function evaluation timed out.
3. SyncRoot – Value: Function evaluation disabled because a previous function evaluation timed out.

How can I try to debug this further to find out what is going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:10:41+00:00Added an answer on May 12, 2026 at 4:10 pm

    To debug further you need to inspect Printers before entering the foreach loop.

    1. Put a breakpoint on the
      searchPrinters.Get() line.
    2. Press F10
    3. Now inspect your item.

    Another thing that should help is changing err.Message to err.ToString().

    For what it’s worth, I debugged this with no problems running both VS 2005 & 2008 on XP.

    EDIT: Posting code for you to try.

    public static List<string> GetPrintersCollection()
    {
        if (printers == null)
        {
            printers = new List<string>();
            string searchQuery = "SELECT * FROM Win32_Printer";
            try
            {
                using (ManagementObjectSearcher searchPrinters = new ManagementObjectSearcher(searchQuery))
                {
                    ManagementObjectCollection Printers = searchPrinters.Get(); // <= Printers data below
                    foreach (ManagementObject printer in Printers)
                    {
                        printers.Add(printer.Properties["Name"].Value.ToString());
                    }
                }
            }
            catch (UnauthorizedAccessException err)
            {
                //Log & re-throw
                Console.WriteLine("Caught UnauthorizedAccessException:  " + err.ToString()); 
                throw;  //re-throw existing exception, not a new one
            }
            //there's no reason to catch the plain-old Exception 
        }
    
        return printers;
    }
    

    EDIT: The only other thing I can think of is to manually go through your Printers and Faxes to see if any are causing problems for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 187k
  • Answers 187k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For similar situation where based on lang setting I import… May 12, 2026 at 5:27 pm
  • Editorial Team
    Editorial Team added an answer For a while DTO and VO were the same thing.… May 12, 2026 at 5:27 pm
  • Editorial Team
    Editorial Team added an answer The BookState becomes the intermediate object between the user and… May 12, 2026 at 5:27 pm

Related Questions

I've been scratching my head for quite some time now, this code worked fine
We had a line of code if( !CreateFile( m_hFile, szFile, GENERIC_READ|GENERIC_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL
Ok...so ive been wriggling around with this piece of cod for quite a while
A few days ago, this was my question, and I found the answer. Maybe
I have ASP.Net code similar to the following (this is inside a FIELDSET): <ol>

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.