///<summary>
 ///Find all users profiles on chosen server.
 ///</summary>
 ///<param name="servername">string</param>
 ///<param name="usermask">string</param>
 ///<returns>void</returns>
 private void LoadCheckedListBox(string servername, string usermask)
 {
     int i = 0;
     //is this a student by year search
     Boolean exist = CheckIfStudentByYearExists();
     Boolean existDisabled = CheckIfDisabledAccounts();
     //remove anything already loaded in CheckedListbox
     clb_FileInfo.Items.Clear();
     WmiPropertiesHelper wp = new WmiPropertiesHelper();
     try
     {
         SortedList sl = new SortedList();
         //hashttable for student lists comparison
         Hashtable ht = new Hashtable();
         //returns all users for the server specified - used for
         sl = wp.GetUserList(servername, usermask);
         //student checklist box
         if (exist)
         {
             //yearByStudentSL (key,value)
             //2016,sortedlist of 2016 students
             //2017,sortedlist of 2016 students ...
             ht = GetStudentUserListByYear(yearByStudentSL,this.checkedListBox1);
         }
         ICollection values = sl.GetValueList();
         //narrow list to specific students
         foreach (WmiPropertiesHelper wh in values)
         {
             if (this.tb_user.Text.ToString() != "")
             {
                 Regex r = new Regex(usermask.ToLower());
                 if (r.IsMatch(wh.ToString().ToLower()))
                 {
                     //student year checkbox
                     if (exist)
                     {
                         //check the hashtable for match
                         if (ht.Contains(wh.ToString().ToUpper()))
                         {
                             //if disabled accounts checked then see if the user account is in the adDisabledAccountsHT
                             if (CheckDisabledAccountHashTable(existDisabled, wh.ToString().ToUpper()))
                             {
                                 clb_FileInfo.Items.Add(wh);
                                 i++;
                             }
                         }
                     }
                     else
                     {
                         //if disabled accounts checked then see if the user account is in the adDisabledAccountsHT
                         if (CheckDisabledAccountHashTable(existDisabled, wh.ToString().ToUpper()))
                         {
                             clb_FileInfo.Items.Add(wh);
                             i++;
                         }
                     }
                 }
             }
             else
             {
                 //student checklist box
                 if (exist)
                 {
                     if (ht.Contains(wh.ToString().ToUpper()))
                     {
                         //if disabled accounts checked then see if the user account is in the adDisabledAccountsHT
                         if (CheckDisabledAccountHashTable(existDisabled, wh.ToString().ToUpper()))
                         {
                             clb_FileInfo.Items.Add(wh);
                             i++;
                         }
                     }
                 }
                 else
                 {
                    //if disabled accounts checked then see if the user account is in the adDisabledAccountsHT
                         if (CheckDisabledAccountHashTable(existDisabled, wh.ToString().ToUpper()))
                         {
                             clb_FileInfo.Items.Add(wh);
                             i++;
                         }
                 }
             }
         }
         if (i > 0)
         {
             UpdateStatusStrip(i + " User profiles found");
         }
         else
         {
             UpdateStatusStrip("No user profiles found");
         }
     }
     catch (System.Runtime.InteropServices.COMException ex)
     {
         string mess = "";
         switch ((uint)ex.ErrorCode)
         {
             case 0x80070005:
                 mess = string.Format("{0} Unauthorized System Exceptionn caught.", ex);
                 break;
             case 0x800706BA:
                 mess = string.Format("{0} Unauthorized System Exceptionn caught.", ex);
                 break;
             default:
                 MessageBox.Show("Error opening worksheet: " + ex.Message);
                 break;
         }
         UpdateStatusStrip("Error connecting to " + servername);
         MessageBox.Show(mess, "Server Connection Error",
         MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     //HRESULT cases not returned - will come back later to catch exception
     catch (Exception ex)
     {
         string mess = "";
         switch ((uint)ex.HResult)
         {
             default:
                 mess = string.Format("{0} Exception caught.", ex);
                 break;
         }
         UpdateStatusStrip("Error connecting to " + servername);
         MessageBox.Show(mess, "Server Connection Error",
         MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 ///<summary>
 ///Remove selected user files from server.
 ///</summary>
 ///<returns>void</returns>
 private void RemoveSelectedEntries()
 {
     for (int i = 0; i < clb_FileInfo.Items.Count; i++)
     {
         if (clb_FileInfo.GetItemChecked(i))
         {
             //pass SID from object
             WmiPropertiesHelper wh = new WmiPropertiesHelper();
             wh = (WmiPropertiesHelper)clb_FileInfo.Items[i];
             try
             {
                 wh.DeleteUser(lb_FileSystemOptions.SelectedItem.ToString(), wh.SID);
                 UpdateStatusStrip("Reloading User profile list...");
             }
             catch (Exception e)
             {
                 UpdateStatusStrip("Error removing profile " + wh.LocalPath + " from server " + lb_FileSystemOptions.SelectedItem.ToString());
                 string mess = string.Format("{0} Exception caught.", e);
                 MessageBox.Show(mess, "Error in Profile Delete",
                 MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
         }
     }
 }
 ///<summary> 
 ///Uses System.Management wmi services to retrieve a list of user profiles on the server
 ///</summary>
 ///<param name="server">string</param>
 ///<param name="usermask">string</param>
 ///<returns>SortedList</returns>
 ///<remarks>Returns sortledlist of users that will be displayed in a dialog box</remarks>
 public SortedList GetUserList(string server, string usermask)
 {
     SortedList sl = new SortedList();
     try
     {
         ManagementScope scope = new ManagementScope("\\\\" + server + "\\root\\cimv2");
         scope.Connect();
         ObjectQuery query = new ObjectQuery(String.Format("SELECT * FROM Win32_UserProfile"));
         ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
         ManagementObjectCollection queryCollection = searcher.Get();
         foreach (ManagementObject m in queryCollection)
         {
             string sid = (string)m.GetPropertyValue("SID");
             string lp = (string)m.GetPropertyValue("LocalPath");
             status = (uint)m.GetPropertyValue("Status");
             if (lp.Contains("C:\\Users\\"))
             {
                 WmiPropertiesHelper wp = new WmiPropertiesHelper();
                 wp.LocalPath = lp.Replace("C:\\Users\\","");
                 wp.SID = sid;
                 wp.Status = status;
                 sl.Add(lp, wp);
             }
         }
     }
     catch (Exception e)
     {
         //re-throw exception for main calling
         throw new Exception("WMIPropertiesHelper", e);
     }
     return (sl);
 }