0

I'm working on a VB.Net application which is running on Windows Mobile handhelds. I'm using .Net compact framework 3.5. The particular issue I'm experiencing has been replicated on 3 different devices from different manufacturers, one with WM 6.1 and two with WM 5. I am trying to delete a registry key using the code below:

Public Shared Sub DeleteRegistryKey(ByVal RootKey As Microsoft.Win32.RegistryKey, ByVal KeyName As String)
  Try
    Dim RegistryKey As RegistryKey = RootKey.OpenSubKey("Software\COMPANY\Printers\", True)
    RegistryKey.DeleteSubKey(KeyName, True)
    RegistryKey.Flush()
    RegistryKey.Close()
  Catch ex As Exception
    MobileLogging.LogException(ex)
  End Try
End Sub

If I do a soft restart of the device, the code will run perfectly once, and delete the selected key (I have verified this with a registry editor). If I run it again I get an "unauthorizedaccessexception" error. This happens with and without the flush and close calls at the end. I have searched and searched, but have found no solution. Any help would be greatly appreciated.

user3900520
  • 71
  • 1
  • 2
  • 7
  • 1
    Does it still exist the second time you attempt to delete it (i.e. are you trying to delete a key that is no longer there)? – ctacke Aug 02 '14 at 20:23
  • 1
    a) the key is still 'open': what is about closing Rootkey passed in as agrument? b) the key does not exist. The registry class and objects are messy within compact framework and I solved similar issues by using p/invoke for all registry access and changes. – josef Aug 03 '14 at 06:28
  • The second time I delete it it is there (I recreate it first). I've tried closing the rootkey, no cigar. I have not tried p/invoke though. Maybe that's what I need. Will try and report back. Thanks. (Sorry for my lateness, I appreciate the helpful responses) – user3900520 Aug 28 '14 at 16:19
  • I wasn't thinking right when I said I recreate the key first. What actually is happening is the second time I use the delete routine I am trying to delete a different key (so if I deleted printer 1 I then try to delete printer 2). – user3900520 Aug 28 '14 at 16:26

0 Answers0