Search code examples
c#printingnetworkcredentials

Passing credentials when using PrintServer


I'm using print server:

PrintServer myPrintServer = new PrintServer(@"\\printsrv");

but I get exception:

An exception occurred while creating the PrintServer object. Win32 error: The user name or password is incorrect.

How can I pass username and password?


Solution

  • You'll need to have the network credentials cached for that server. I think the easiest way is to start a net use command from code:

    var p = new Process();
    p.StartInfo.FileName = "net";
    p.StartInfo.Arguments = "use \\printsrv password /user:domain\username";
    p.StartInfo.CreateNoWindow = true;
    p.Start();
    p.WaitForExit();
    

    After that the PrintServer class should be able to connect. You could also try these answers to get the credentials cached beforehand.