My app handles its own SFTP HostKey management; if the WinSCP application is installed on the same computer and the user elects to cache a HostKey, this can cause problems for my app. The user understands that the app's HostKey management takes precedence over WinSCP's.
Thus I've gone to extra measures to make sure that on startup the app clears the HostKey from the registry and also deletes any existing WinScp.ini files in the execution directory from previous sessions. I'm including my registry access code below to demonstrate.
My ad-hoc solution works for now, but my concern is that it's a bit brittle. In other words, it's relying on undocumented architecture. If this architecture should change in the future it could break my design.
It would be helpful if the .NET API could include a method to accomplish the task if desired. This way it would be documented, stable and more reliable.
Private Sub RemoveCachedHostKey(HostName As String)
oAppResult As Action(Of String)
oAppQuery As Func(Of String, Boolean)
oAppKey As RegistryKey
oHostsKey = Nothing
oAppKey = Nothing
oHostsResult = Sub(ValueName As String) oHostsKey.DeleteValue(ValueName)
oHostsQuery = Function(ValueName As String) ValueName.ToLower.EndsWith(HostName.ToLower)
oAppResult = Sub(KeyName As String)
oHostsKey = oAppKey.OpenSubKey(KeyName, True).OpenSubKey("SshHostKeys", True)
If oHostsKey IsNot Nothing Then
If oHostsKey.ValueCount = 0 Then
oAppQuery = Function(KeyName As String) KeyName.StartsWith("WinSCP")
With RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey("Software", True)
oAppKey = .OpenSubKey("Martin Prikryl", True)
If oAppKey IsNot Nothing Then