开发者

PrinterSettings.IsValid always returning false

In our code, we have to give the users a list of printers to choose from. The user then chooses a printer and it is checked to verify it is valid before printing. On a windows 2003 server with IIS 6, this works fine. On a windows 2008 server with IIS 7, it fails each time impersonate is set to true.

PrinterSettings printerSetting = new PrinterSettings();
printerSetting.PrinterName = ddlPrinterName.SelectedItem.Text;
if (!printerSetting.IsValid)
{
    lblMsg.Text = "Server Printer is not valid.";
}
else
开发者_开发技巧{
    lblMsg.Text = "Success";
}

Each time this code is run, the "Server Printer is not valid" displays, only if impersonate is set to true. If impersonate is set to false, the success message is displayed.

The impersonation user has full rights to the printer.

Is there a way to catch the actual reason the printer is not valid? Is there some other 2008 setting I should check?

update I found that IsValid fails when the IIS7 application pools has "Enable 32-bit applications" is checked. This must be checked b/c we are using a 3rd party tool to print with, and it is a 32-bit application. It is not currently part of this test, so right now it is not causing this error.


IIS 7.0 is really locked down. It sounds like the server is not impersonating properly. The printer profiles are stored in the HK_CURRENT_USER hive of the user or if it is a locally connected printer in the HK_LOCAL_MACHINE.

I would use PROCMON from SYSINTERNALS to see the calls the IIS process is making.


You can try by querying the System using WMI. A way to achive this may be the following:

using System.Management;

   private List<string> GetPrinters()
   {     
    List<string> printerNames = new List<string>();

    System.Management.ObjectQuery oquery = 
        new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");

    System.Management.ManagementObjectSearcher mosearcher = 
        new System.Management.ManagementObjectSearcher(oquery);

    System.Management.ManagementObjectCollection moc = mosearcher.Get();

    foreach (ManagementObject mo in moc)
    {
        System.Management.PropertyDataCollection pdc = mo.Properties;
        foreach (System.Management.PropertyData pd in pdc)
        {
            if ((bool)mo["Network"])
            {
                printerNames.Add(mo[pd.Name]);
            }
        }
    }

    return printerNames;
}

After that, in a similar way, you may find other printer informations as if the printer is ready. Find more here: https://stackoverflow.com/a/1622931/2791580

Regards


Applications Pool Advanced Setting Process Model Change Identity to User Administrators

oPD.PrinterSettings.PrinterName = \\10.10.1.1\myprintertnetwork;


I've had exactly the same problem, and I was able to solve it by temporarily leaving the impersonation context. Adapting your example, the following code:

PrinterSettings printerSetting = new PrinterSettings();
printerSetting.PrinterName = ddlPrinterName.SelectedItem.Text;

using (var wic = WindowsIdentity.Impersonate(IntPtr.Zero))
{
    if (!printerSetting.IsValid)
    {
        lblMsg.Text = "Server Printer is not valid.";
    }
    else
    {
        lblMsg.Text = "Success";
    }

    // Do the remainder of your printing stuff here, but beware that
    // your user context is different.
}

should yield the success message. (Credit for this solution goes to Jon Saffron.)


try set a default printer on [ control panel - devices and printers right click on one the ready printers and set as default it ]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜