Existe-t-il un moyen d'imprimer une page de test standard dans WPF à partir du code C# si j'ai le nom d'une imprimante réseau?Puis-je imprimer une page de test standard dans code-behind dans .NET 3.5?
Merci!
Existe-t-il un moyen d'imprimer une page de test standard dans WPF à partir du code C# si j'ai le nom d'une imprimante réseau?Puis-je imprimer une page de test standard dans code-behind dans .NET 3.5?
Merci!
Voici un exemple d'utilisation de l'espace de noms System.Management pour accéder WMI et imprimer une page de test à une imprimante. Cela dépend de l'imprimante étant connectée à l'ordinateur, je peux fournir le code pour connecter une imprimante réseau via System.Management si vous le souhaitez aussi. Ce code devrait fonctionner pour toute version du Framework .Net
using System;
using System.Management;
public class PrintTestPageUsingWMI
{
private String _name;
private ManagementObject _printer = null;
public PrintTestPageUsingWMI(String printerName)
{
this._name = printerName;
//Find the Win32_Printer which is a Network Printer of this name
//Declare WMI Variables
ManagementObject MgmtObject;
ManagementObjectCollection MgmtCollection;
ManagementObjectSearcher MgmtSearcher;
//Perform the search for printers and return the listing as a collection
MgmtSearcher = new ManagementObjectSearcher("Select * from Win32_Printer");
MgmtCollection = MgmtSearcher.Get();
foreach (ManagementObject objWMI in MgmtCollection)
{
if (objWMI.Item("sharename").ToString().Equals(this._name))
{
this._printer = objWMI;
}
}
if (this._printer == null)
{
throw new Exception("Selected Printer is not connected to this Computer");
}
}
public void PrintTestPage()
{
this.InvokeWMIMethod("PrintTestPage");
}
/// <summary>
/// Helper Method which Invokes WMI Methods on this Printer
/// </summary>
/// <param name="method">The name of the WMI Method to Invoke</param>
/// <remarks></remarks>
private void InvokeWMIMethod(String method) {
if (this._printer == null)
{
throw new Exception("Can't Print a Test Page on a Printer which is not connected to the Computer");
}
Object[] objTemp = new Object[0] { null };
ManagementObject objWMI;
//Invoke the WMI Method
this._printer.InvokeMethod(method, objTemp);
}
}
Sinon, vous pouvez regarder l'espace de noms System.Printing qui est pris en charge .Net 3.0 et versions ultérieures
Il devrait être possible d'utiliser prnadmin.dll. Vous devrez très probablement d'abord créer l'imprimante dans Windows (soit par code ou avec l'interface utilisateur) si elle n'est pas déjà configurée sur le poste de travail.
http://support.microsoft.com/kb/321025
http://www.codeproject.com/KB/cs/PrinterAdmin.aspx?display=Print