windows C#/.NET:如何将联网打印机添加到本地 PC 帐户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6963152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#/.NET: How to add networked printer to a local PC account?
提问by JohnnyRockets
I'm using the WMI Code Creator to create code to add a networked printer.
我正在使用 WMI Code Creator 创建代码以添加联网打印机。
http://img13.imageshack.us/img13/9847/wmicodecreatorwin32prin.png
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.
但是,我需要将联网打印机添加到本地 PC 帐户,即无法访问 \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 currentPC, not to a remote PC.) (The caveat is that the logged on user is a local PC account.)
(我需要将远程打印机添加到当前PC,而不是远程 PC。)(需要注意的是,登录用户是本地 PC 帐户。)
Does anyone know how this can be achieved?
有谁知道这是如何实现的?
回答by Justin C
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.
即 pc1 在本地有用户 bob1。使 bob1 成为打印服务器上的用户。
run your map program as bob1 on pc1 and you should be able to get to the printer.
在 pc1 上以 bob1 的身份运行地图程序,您应该能够访问打印机。
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.
否则,网络打印机是每个用户...以具有访问权限的域用户(即 runas)运行您的程序将不起作用,因为它只会将打印机映射到用户会话,而不是您实际想要的会话。
...what about this? http://www.codescript.co.uk/wmi_connect_as_another_user.htm
...那这个呢? 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)
...或脚本? http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=12028(即使它不适用于 c#,它仍然可以提供 wmi synatx 的东西)
回答by JMIII
So since other answer was removed. I just tested this and it worked. Here is link to get the class I am using https://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-UserHere is the code below which worked for me. If you look there are some even better implementations of classes out there to impersonate users.
因此,由于其他答案已被删除。我刚刚测试了这个并且它起作用了。这是获取我正在使用的课程的链接https://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User下面是对我有用的代码。如果您看一下,那里有一些更好的类实现来模拟用户。
static void Main(string[] args)
{
using (new Impersonator("username", "domainName", "myPassword"))
{
// The following code is executed under the impersonated user.
AddPrinterUnc(@"\PrintServer\printershare");
}
}
public static void AddPrinterUnc(string printUncPath)
{
using (ManagementClass oPrinterClass = new ManagementClass(new ManagementPath("Win32_printer"), null))
{
using (ManagementBaseObject oInputParameters = oPrinterClass.GetMethodParameters("AddPrinterConnection"))
{
oInputParameters.SetPropertyValue("Name", printUncPath);
oPrinterClass.InvokeMethod("AddPrinterConnection", oInputParameters, null);
}
}
}