我正在使用WMI代码创build器创build代码来添加联网的打印机。
http://img13.imageshack.us/img13/9847/wmicodecreatorwin32prin.png
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); } } } }
但是,我需要将networking打印机添加到本地PC帐户,即无权访问 PrintServer的非域帐户。
运行WPF程序时防止Windows工作站(桌面)locking
编程添加路线
.NET进程间“事件”
带有通配符的.NET DirectoryInfo.GetFiles在不同的驱动器上有不同的含义
我在哪里可以把域用户的(服务帐户)用户名和密码放到上面的代码中?
我一直在谷歌search几个小时,但我能find的是一个愚蠢的职位,说怎么在远程机器上添加打印机,这不是我想要做的。
(我需要将远程打印机添加到当前 PC,而不是远程PC。)(注意,login用户是本地PC帐户。)
有谁知道这是如何实现的?
如何以编程方式跟随.lnk文件
如何在安装程序下载时在.NET安装程序中embedded用户特定的数据?
如何用命令行压缩指定的文件夹
监视文件/目录访问在C#
如何使我的服务停止自动发布到Windows应用程序事件日志
您可以在打印服务器上创建相同的本地帐户,通过这样做来启用对等身份验证…
即pc1在本地有用户bob1。 使bob1成为打印服务器上的用户。
在pc1上运行你的地图程序bob1,你应该能够到达打印机。
这有帮助吗?
否则,网络打印机是每个用户…运行你的程序作为具有访问权限(即跑步)的域用户将无法正常工作,因为它只是将打印机映射到用户会话,而不是你真正想要的。
…那这个呢? http://www.codescript.co.uk/wmi_connect_as_another_user.htm
…或scriptomatic? http://www.microsoft.com/download/en/details.aspx?displayLang=en&id=12028 (即使这是不是为C#,它仍然可以给WMI synatx的东西)
所以,因为其他答案被删除。 我只是测试这个,它的工作。 这里是链接,以获得我正在使用的类我使用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(@"\Printserverprintershare"); } } 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); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。