none
System.Printing .Net 4.5 RRS feed

  • Question

  • I need to enumerate printers (print queues) on a remote server and the following code from msdn doesn't do anything at all. I hijacked the code from MSDN (http://msdn.microsoft.com/en-us/library/ms552937.aspx) by all accounts it *should* work, but the PrintServer object myPrintServer doesn't exist. not the actual server, the object in code. every reference after the first line has a red squiggly under it.

    PrintServer myPrintServer = new PrintServer(@"\\printserver.ku.edu");
                
    // List the print server's queues
    PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
    String printQueueNames = "My Print Queues:\n\n";
                
    foreach (PrintQueue pq in myPrintQueues)
    {
        printQueueNames += "\t" + pq.Name + "\n";
    }
                
    Console.WriteLine(printQueueNames);
    Console.WriteLine("\nPress Return to continue.");
    Console.ReadLine();


    Jeffrey S. Patton Jeffrey S. Patton Systems Specialist, Enterprise Systems University of Kansas 1001 Sunnyside Ave. Lawrence, KS. 66045 (785) 864-0242 | http://patton-tech.com

    Friday, December 7, 2012 4:01 AM

Answers

  • I assume you actually meant to post something like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Printing;
    using System.Management.Automation;
    
    namespace PrintServerManagement
    {
        public class Class1
        {
          public void DoStuff() {
             PrintServer myPrintServer = new PrintServer(@"\\pcutprd.home.ku.edu");
                
             // List the print server's queues
             PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
             String printQueueNames = "My Print Queues:\n\n";
                
             foreach (PrintQueue pq in myPrintQueues)
             {
                printQueueNames += "\t" + pq.Name + "\n";
             }
                
             Console.WriteLine(printQueueNames);
             Console.WriteLine("\nPress Return to continue.");
             Console.ReadLine();
          }
       }
    Do you have System.Management.Automation properly referenced from the DLL here:
    C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0?

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.


    Friday, December 7, 2012 3:39 PM

All replies

  • Hi,
    that means you have compile time error . Have you added reference to System.Printing assembly and used appropiate namespace in your code file.
    One more thing , the msdn code says "theServer" must be a print server to which the user has full print access. Do you have right to  access that printer ?
    If issue does not resolve can you please show more of your codes?

    One good question is equivalent to ten best answers.

    Friday, December 7, 2012 7:24 AM
  • Bootstrap,

    I have System.Printing referenced in my app, in addition i tried referencing the dll directly through that GAC folder.

    The thing is, that first line "appears" to be ok, but I can't refernce the variable afterwards. when i start to type myprintserver i get no auto-complete on it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Printing;
    using System.Management.Automation;
    
    namespace PrintServerManagement
    {
        public class Class1
        {
            PrintServer myPrintServer = new PrintServer(@"\\pcutprd.home.ku.edu");
                
            // List the print server's queues
            PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
            String printQueueNames = "My Print Queues:\n\n";
                
            foreach (PrintQueue pq in myPrintQueues)
            {
                printQueueNames += "\t" + pq.Name + "\n";
            }
                
            Console.WriteLine(printQueueNames);
            Console.WriteLine("\nPress Return to continue.");
            Console.ReadLine();
       }
    }


    Jeffrey S. Patton Jeffrey S. Patton Systems Specialist, Enterprise Systems University of Kansas 1001 Sunnyside Ave. Lawrence, KS. 66045 (785) 864-0242 | http://patton-tech.com

    Friday, December 7, 2012 3:25 PM
  • Hi,

    i remember back in time , i have also faced these kind of issue . Have you restarted your VS or done a complete System restart ?


    One good question is equivalent to ten best answers.

    Friday, December 7, 2012 3:30 PM
  • Interesting...I've not rebooted in a while. I'm in a meeting for another hour, I'll reboot after and update here.

    Jeffrey S. Patton Jeffrey S. Patton Systems Specialist, Enterprise Systems University of Kansas 1001 Sunnyside Ave. Lawrence, KS. 66045 (785) 864-0242 | http://patton-tech.com

    Friday, December 7, 2012 3:31 PM
  • I assume you actually meant to post something like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Printing;
    using System.Management.Automation;
    
    namespace PrintServerManagement
    {
        public class Class1
        {
          public void DoStuff() {
             PrintServer myPrintServer = new PrintServer(@"\\pcutprd.home.ku.edu");
                
             // List the print server's queues
             PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
             String printQueueNames = "My Print Queues:\n\n";
                
             foreach (PrintQueue pq in myPrintQueues)
             {
                printQueueNames += "\t" + pq.Name + "\n";
             }
                
             Console.WriteLine(printQueueNames);
             Console.WriteLine("\nPress Return to continue.");
             Console.ReadLine();
          }
       }
    Do you have System.Management.Automation properly referenced from the DLL here:
    C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0?

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.


    Friday, December 7, 2012 3:39 PM
  • GAH! I'm so stupid..I should really learn to not code when I'm very tired! Thank you, I honestly can't believe i did that. To answer you other question, yes powershell is registered properly.

    Jeffrey S. Patton Jeffrey S. Patton Systems Specialist, Enterprise Systems University of Kansas 1001 Sunnyside Ave. Lawrence, KS. 66045 (785) 864-0242 | http://patton-tech.com

    Friday, December 7, 2012 3:42 PM
  • Can I take it that my post is the answer then?   ;-)

    We all make such mistakes now and then; some of us just don't (like to) admit it in public.


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.


    Friday, December 7, 2012 5:52 PM
  • Indeed! I actually try to admit when I foul up, I mean we're all human for the most part ;-) so most of us make mistakes unfortunately...lol

    Jeffrey S. Patton Jeffrey S. Patton Systems Specialist, Enterprise Systems University of Kansas 1001 Sunnyside Ave. Lawrence, KS. 66045 (785) 864-0242 | http://patton-tech.com


    Friday, December 7, 2012 5:54 PM