Установка принтера по умолчанию

/// 
/// method to set a specified printer as the system's default printer
/// 
/// name of the printer we want to be default/// Returns true if successfull
/// Throws exception if printer not installed
public bool SetPrinterToDefault(string printer)
{
    //path we need for WMI
    string queryPath = "win32_printer.DeviceId='" + printer + "'";

    try
    {
        //ManagementObject for doing the retrieval
        ManagementObject managementObj = new ManagementObject(queryPath);

        //ManagementBaseObject which will hold the results of InvokeMethod
        ManagementBaseObject obj = managementObj.InvokeMethod("SetDefaultPrinter", null, null);

        //if we're null the something went wrong
        if (obj == null)
            throw new Exception("Unable to set default printer.");

        //now get the return value and make our decision based on that
        int result = (int)obj.Properties["ReturnValue"].Value;

        if (result == 0)
            return true;
        else
            return false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return false;
    }
}
Взято с http://www.dreamincode.net


Комментариев нет:

Отправить комментарий

Большая просьба, не писать в комментариях всякую ерунду не по теме!