C#/.NET: How to access a network printer share with another user's credentials?
In Windows, when using a local PC account and trying to access the network printer share (ie typing "\printershare" in the Run window), a box pops up asking for a d开发者_运维问答omain user's username and password.
Put in a domain user's username and password, and then the printer share appears using the domain user's credentials.
How can this be achieved via C#?
I have tried the following methods:
var printerShare = new DirectoryEntry(@"\\printershare\", "username", "password");
var printerShare = new DirectoryEntry(@"WinNT://printershare\", "username", "password");
var printerShare = new DirectoryEntry(@"WinNT:\\printershare\", "username", "password");
var printerShare = new DirectoryEntry("LDAP://printershare.corp.domain.com", "username", "password");
var printerShare = new DirectoryEntry("LDAP:\\printershare.corp.domain.com", "username", "password");
Unfortunately, none of them work. Mapping a network directory or folder would be simple.
But the fact that there are no folders is what is making me scratch my head.
Anyone know what I'm doing wrong here?
Check out this question (specifically the NetworkConnection class):
How to provide user name and password when connecting to a network share
精彩评论