using System.Management;
private static ManagementScope oManagementScope = null;
private void Form1_Load(object sender, EventArgs e)
{
AddPrinter("\\\\IP\\Letter");
MessageBox.Show("Add");
DeletePrinter("\\\\IP\\Letter");
}
public static bool AddPrinter(string sPrinterName)
{
try
{
oManagementScope = new ManagementScope(ManagementPath.DefaultPath);
oManagementScope.Connect();
ManagementClass oPrinterClass = new ManagementClass
(new ManagementPath("Win32_Printer"), null);
ManagementBaseObject oInputParameters =
oPrinterClass.GetMethodParameters("AddPrinterConnection");
oInputParameters.SetPropertyValue("Name", sPrinterName);
oPrinterClass.InvokeMethod("AddPrinterConnection", oInputParameters, null);
return true;
}
catch (Exception )
{
return false;
}
}
public static bool DeletePrinter(string sPrinterName)
{
oManagementScope = new ManagementScope(ManagementPath.DefaultPath);
oManagementScope.Connect();
SelectQuery oSelectQuery = new SelectQuery();
oSelectQuery.QueryString = @"SELECT * FROM Win32_Printer WHERE Name = '" +
sPrinterName.Replace("\\", "\\\\") + "'";
ManagementObjectSearcher oObjectSearcher =
new ManagementObjectSearcher(oManagementScope, oSelectQuery);
ManagementObjectCollection oObjectCollection = oObjectSearcher.Get();
if (oObjectCollection.Count != 0)
{
foreach (ManagementObject oItem in oObjectCollection)
{
oItem.Delete();
return true;
}
}
return false;
}
- Jun 20 Mon 2011 08:22
C#程式碼建立印表機
全站熱搜
留言列表