tags:

views:

37

answers:

3

hi

how to print Test-page on default Printer

using C# Winform Code ?

thank's in advance

+1  A: 

Use WMI, specifically, the PrintTestPage method of the Win32_Printer class.

See this MSDN article for how to execute a WMI method from C#: How To: Execute a Method

ho1
A: 

As ho1 said the WMI can accommodate you:

ManagementClass processClass = new ManagementClass("Win32_Printer");
ManagementBaseObject outP = processClass.InvokeMethod("PrintTestPage", null);
if (Convert.ToUInt32(outP["ReturnValue"]) != 0)
{
    MessageBox.Show("Failed to print test page.");
}

Whenever you call print on a PrintDocument without specifying a printer it will of course use the default:

PrintDocument doc = new PrintDocument();
doc.Print(); // will print a blank page
Jack B Nimble
A: 

To generate the built-in Windows test page, you can also use p/invoke against PrintUI.dll. Here's a simple class which lets you do this:

public static class PrintTestPageHelper
{
    [DllImport("printui.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern void PrintUIEntryW(IntPtr hwnd, 
        IntPtr hinst, string lpszCmdLine, int nCmdShow);

    /// <summary>
    /// Print a Windows test page.
    /// </summary>
    /// <param name="printerName">
    /// Format: \\Server\printer name, for example:
    /// \\myserver\sap3
    /// </param>
    public static void Print(string printerName)
    {
        var printParams = string.Format(@"/k /n{0}", printerName);
        PrintUIEntryW(IntPtr.Zero, IntPtr.Zero, printParams, 0);
    }
}

public class Program
{

    static void Main(string[] args)
    {
        PrintTestPageHelper.Print(@"\\printserver.code4life.com\sap3");

        Console.WriteLine("Press enter to exit.");
        Console.ReadLine();
    }

}
code4life