0

I want to remove a Printer from a Windows account. This will be used via Citrix. First I want to retrieve all printers that are installed for the user and then I want to remove a printer.

I am using the following code to do this. This works on a normal PC. But when I use this via Citrix then it does not work. Not all Printers are retrieved via this method. Also I cannot remove the Printer. Does somebody know why? What can I do to use this via Citrix? What is different when using this via Citrix?

using System.Collections.Generic;
using System.Linq;
using System.Management;

namespace RemovePrinter
{
    public class PrinterManager
    {
        public List<string> GetInstalledPrinters()
        {
            var managementScope = new ManagementScope(ManagementPath.DefaultPath);
            managementScope.Connect();

            var selectQuery = new SelectQuery {QueryString = @"SELECT * FROM Win32_Printer"};

            var objectSearcher = new ManagementObjectSearcher(managementScope, selectQuery);
            var ojectCollection = objectSearcher.Get();

            return (from ManagementBaseObject item in ojectCollection select item["Name"].ToString()).ToList();
        }

        public bool DeletePrinter(string printerName)
       {
           var managementScope = new ManagementScope(ManagementPath.DefaultPath);
           managementScope.Connect();

           var selectQuery = new SelectQuery
           {
               QueryString = @"SELECT * FROM Win32_Printer WHERE Name = '" +
                          printerName.Replace("\\", "\\\\") + "'"
           };

           var ojectSearcher = new ManagementObjectSearcher(managementScope, selectQuery);
           var ojectCollection = ojectSearcher.Get();

           if (ojectCollection.Count == 0) return false;

           foreach (var item in ojectCollection.Cast<ManagementObject>())
           {
               item.Delete();
               return true;
           }

           return false;
       }
   }
}

1 Answer 1

1

ManagementObjectSearcher is a part of WMI API classes. By default these services are not enabled on Citrix and that is the reason why it does not work. You need to have the right services installed as well have license to use those. Check this out "http://support.citrix.com/article/ctx116423"

2
  • We are using XenApp 7.6. It seems to work differently in this version. We could not find this service. Do you know how it works in XenApp 7.6?
    – Alexander
    Apr 16, 2015 at 8:36
  • 1
    Please check if WMI Provider feature is installed. If not, it can be found on support/DirectorWMIProvider folder of your installation DVD. If this is installed make sure WinRM is enabled on your server. You can use WinRm qc command to check if its ON or not. Apr 16, 2015 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.