Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/.NET: How to add networked printer to a local PC account?

I'm using the WMI Code Creator to create code to add a networked printer.

http://img13.imageshack.us/img13/9847/wmicodecreatorwin32prin.png

The code that was generated works great (under my domain account anyway):

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class CallWMIMethod
    {
        public static void Main()
        {
            try
            {
                ManagementClass classInstance =
                    new ManagementClass("root\\CIMV2",
                    "Win32_Printer", null);

                // Obtain in-parameters for the method
                ManagementBaseObject inParams =
                    classInstance.GetMethodParameters("AddPrinterConnection");

                // Add the input parameters.
                inParams["Name"] =  "\\\\PrintServer\\PrinterName";

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams =
                    classInstance.InvokeMethod("AddPrinterConnection", inParams, null);

                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
            }
        }
    }
}

However, I need to add a networked printer to a local PC account, ie a non-domain account that does not have access to \PrintServer.

Where can I put in a domain user's (a service account) username and password into the above code?

I have been Googling for hours but all I can find is that one stupid post that says how to add a printer on a remote machine, which is not what I'm looking to do.

(I need to add a remote printer to the current PC, not to a remote PC.) (The caveat is that the logged on user is a local PC account.)

Does anyone know how this can be achieved?

like image 522
JohnnyRockets Avatar asked Aug 05 '11 22:08

JohnnyRockets


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

you could create the same local account on the print server, enabling peer-to-peer authentication by doing so...

i.e. pc1 has user bob1 locally. make bob1 a user on the print server.

run your map program as bob1 on pc1 and you should be able to get to the printer.

does that help?

otherwise, network printers are per user...running your program as the domain user that has access (i.e. runas) wouldn't work since it would simply map the printer to the users session and not the one you actually want.

...what about this? http://www.codescript.co.uk/wmi_connect_as_another_user.htm

...or scriptomatic? http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=12028 (even tho it is not for c#, it can still give wmi synatx stuff)

like image 149
Justin C Avatar answered Sep 18 '22 14:09

Justin C