我是C#的新手。
我正在尝试使用this post I found以编程方式安装打印机,但我在此处获得例外:
inputParam.SetPropertyValue("Print", "\\\\http://ip.of.my.server");
在System.Management.dll中找到了“System.Management.ManagementException”类型的异常。
其他信息:未找到。
以下是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
namespace Printer
{
class Installation
{
static public string addprinter()
{
ManagementClass win32Printer = new ManagementClass("Win32_Printer");
ManagementBaseObject inputParam = win32Printer.GetMethodParameters("AddPrinterConnection");
inputParam.SetPropertyValue("Print", "\\\\http://ip.of.my.server");
ManagementBaseObject result = (ManagementBaseObject)win32Printer.InvokeMethod("AddPrinterConnection", inputParam, null);
uint errorCode = (uint)result.Properties["returnValue"].Value;
switch (errorCode)
{
case 0:
return ("Successfully connected printer.");
case 5:
return ("Access Denied.");
case 123:
return ("The filename, directory name, or volume label syntax is incorrect.");
case 1801:
return ("Invalid Printer Name.");
case 1930:
return ("Incompatible Printer Driver.");
case 3019:
return("The specified printer driver was not found on the system and needs to be downloaded.");
}
return ("Unkown error.");
}
}
}
这是调用代码的表单,虽然我不认为问题存在:
private void Send_Click(object sender, EventArgs e)
{
if (true)
{
MessageBox.Show(Installation.addprinter());
}