joe_pool_is
Contributor
I recently had to open a project because someone on our production floor did not know how to navigate the menus. When I tried to run it, I got an Unauthorized Access Exception for code that I had not modified.
This query worked before installing VS2008, but now it will not run in a test application under VS2005 either.
My code fails in the foreach loop when the printer is assigned at this line:
Any ideas why?
Help! Please, and Thank You!
This query worked before installing VS2008, but now it will not run in a test application under VS2005 either.
My code fails in the foreach loop when the printer is assigned at this line:
(ManagementObject printer in Printers)
Code:
public static void GetPrintersCollection() {
try {
string sql = "SELECT * FROM Win32_Printer";
using (ManagementObjectSearcher query = new ManagementObjectSearcher(sql)) {
ManagementObjectCollection Printers = query.Get();
foreach (ManagementObject printer in Printers) {
Console.WriteLine(printer.Properties["Name"].Value.ToString());
}
}
} catch (UnauthorizedAccessException err) { // err.Message is empty
Console.WriteLine("You do not have authorization to access this printer.");
} catch (Exception err) {
Console.WriteLine(err.Message);
}
}
Any ideas why?
Help! Please, and Thank You!