/// <summary> /// ''' Delete the value from the registry. Name may be an empty string for the unnamed value. Value will be deleted from the subkey supplied. /// ''' </summary> /// ''' <param name="DriverID">ProgID of the device to read from</param> /// ''' <param name="Name">Name of the variable whose value is retrieved</param> /// ''' <param name="SubKey">Subkey from the profile root in which to write the value</param> /// ''' <exception cref="Exceptions.InvalidValueException">Thrown if DriverID is an empty string.</exception> /// ''' <exception cref="Exceptions.DriverNotRegisteredException">Thrown if the driver is not registered,</exception> /// ''' <remarks>Specify "" to delete the unnamed value which is also known as the "default" value for a registry key. </remarks> public new void DeleteValue(string DriverID, string Name, string SubKey) { // Delete a value TL.LogMessage("DeleteValue", "Driver: " + DriverID + " Name: " + Name + " Subkey: \"" + SubKey + "\""); CheckRegistered(DriverID); ProfileStore.DeleteProfile(MakeKey(DriverID, SubKey), Name); }
/// <summary> /// Completely remove drivers flagged for deletion /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnDeleteDrivers_Click(object sender, EventArgs e) { // Test whether no devices have been selected and show a message if (dynamicDriversCheckedListBox.CheckedItems.Count == 0) { MessageBox.Show("No drivers have been selected for deletion", "Delete Drivers", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // Confirm whether the user really does want to delete the selected drivers DialogResult result = MessageBox.Show("Are you sure that you want to delete the checked drivers?", "Delete Dynamic Drivers", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result != DialogResult.Yes) { return; // Give up if there is any outcome other than yes } try { // Disable controls so that the process can't be stopped part way through BtnDeleteDrivers.Enabled = false; // Create pointer to the dynamic driver's local server folder string localServerPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86) + SharedConstants.ALPACA_CLIENT_LOCAL_SERVER_PATH; TL.LogMessage("DeleteDrivers", $"Local server path: {localServerPath}"); // Iterate over each device that has been checked in the UI checked list box foreach (DynamicDriverRegistration driver in dynamicDriversCheckedListBox.CheckedItems) { TL.LogMessage("DeleteDrivers", $"Deleting driver {driver.Description}"); // Create pointers to the driver executable and its PDB file string driverFileName = $"{localServerPath}{driver.ProgId}.dll"; string pdbFileName = $"{localServerPath}{driver.ProgId}.pdb"; TL.LogMessage("DeleteDrivers", $"DLL path: {driverFileName}"); TL.LogMessage("DeleteDrivers", $"PDB path: {pdbFileName}"); // Test whether we can get an exclusive write lock on the DLL and PDB files. If this works we can delete the files later, if not be have to abandon removing this driver if (FileIsAccessible(driverFileName) & FileIsAccessible(pdbFileName)) // Both files are accessible so proceed with removal { // Unregister the driver for COM interop ComUnRegister(driver.ProgId); // Delete the driver file TL.LogMessage("DeleteDrivers", $"Deleting driver files {driverFileName} and {pdbFileName}"); try { File.Delete(driverFileName); TL.LogMessage("DeleteDrivers", $"Successfully deleted driver file { driverFileName}"); } catch (UnauthorizedAccessException ex) { string errorMessage = $"Unable to delete driver file {driverFileName} because it is locked or in use."; TL.LogMessageCrLf("DeleteDrivers", $"{errorMessage} \r\n{ex.ToString()}"); MessageBox.Show(errorMessage, "Alpaca Dynamic Client Manager", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { string errorMessage = $"Unable to delete driver file {driverFileName} - {ex.Message}"; TL.LogMessageCrLf("DeleteDrivers", $"{errorMessage} \r\n{ex.ToString()}"); MessageBox.Show(errorMessage, "Alpaca Dynamic Client Manager", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Delete the driver's PDB file try { File.Delete(pdbFileName); TL.LogMessage("DeleteDrivers", $"Successfully deleted driver file { driverFileName}"); } catch (Exception ex) { string errorMessage = $"Unable to delete driver file {pdbFileName} - {ex.Message}"; TL.LogMessageCrLf("DeleteDrivers", $"{errorMessage} \r\n{ex.ToString()}"); MessageBox.Show(errorMessage, "Alpaca Dynamic Client Manager", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Remove the ASCOM Profile information that is not removed by the local server when the driver is unregistered try { TL.LogMessage("DeleteDrivers", $"Removing driver Profile registration for {driver.DeviceType} driver: {driver.ProgId}"); profile.DeviceType = driver.DeviceType; profile.Unregister(driver.ProgId); } catch (Exception ex) { string errorMessage = $"Unable to unregister driver {driver.ProgId} - {ex.Message}"; TL.LogMessageCrLf("DeleteDrivers", $"{errorMessage} \r\n{ex.ToString()}"); MessageBox.Show(errorMessage, "Alpaca Dynamic Client Manager", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Remove the Chooser flag indicating that this driver has been configured try { using (RegistryAccess registryAccess = new RegistryAccess()) { registryAccess.DeleteProfile("Chooser", $"{driver.ProgId} Init"); } } catch (Exception ex) { string errorMessage = $"Unable to remove driver initialised flag for {driver.ProgId} - {ex.Message}"; TL.LogMessageCrLf("DeleteDrivers", $"{errorMessage} \r\n{ex.ToString()}"); MessageBox.Show(errorMessage, "Alpaca Dynamic Client Manager", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else // One or other of the files is not accessible so we have to omit removal of the driver { string errorMessage = $"Unable to delete the {driver.Name} driver.\r\n\n" + $"This may be because it is still in use by a client application.\r\n\n" + $"Please close all ASCOM applications and try again using the .NET Chooser in the ASCOM Diagnostics application's Tools menu."; MessageBox.Show(errorMessage, "Alpaca Dynamic Client Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning); TL.LogMessage("DeleteDrivers", $"{errorMessage}"); } } FindInstalledDrivers(); } catch (Exception ex) { TL.LogMessageCrLf("DeleteDrivers - Exception", ex.ToString()); MessageBox.Show("Sorry, en error occurred during Apply, please report this error message on the ASCOM Talk forum hosted at Groups.Io.\r\n\n" + ex.Message, "ASCOM Dynamic Clients", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Re-enable the delete button BtnDeleteDrivers.Enabled = true; } }