* A namespace isn't used.
* You started to respect the CamelCase, so we must use it for the whole code.
* Describe what does the method do using <summary> tags.
* Dim FileZilla As New StringBuilder has a bad name. Replace it with an adequate name (e.g: RecentServerList).
* Same thing with Password (an adequate name would be HostPassword).
* The Try/Catch isn't useful in this case. Catch is used to show the exception to the user, don't ignore it - fair example:
Code:
Try
' Code
Catch Ex As Exception
MessageBox.Show(Ex.Message) ' Show the error
End Try
Your code is like:
Code:
Try
' Code
Catch {_ As Exception} ' Each character inside the braces is implied - The _ (underscore) means that you won't use the exception index (variable) - Also, the exception is generic as I've already shown
' Do nothing
End Try
* Avoid generic exceptions, always specify the exception, it swallows any error - so it's hard to debug.
*
Code:
Else : Return String.Empty
don't do this. It increases the reading difficulty level.
*
Code:
Return String.Empty
returns an empty string in the case that the file {ApplicationData folder}\FileZilla\recentservers.xml doesn't exist.
You should warn the user that it doesn't exist instead of returning an empty string.
*
Code:
Return FileZilla.ToString
is in the wrong place. It should be inside the "carry handling" to check the path (at the end of If File.Exists(XmlFilePath) Then)
Finally, the clean code would be:
Code:
''' <summary>
''' This function does a recovery for FileZilla
''' </summary>
''' <returns> Returns recovery status/recent server settings </returns>
''' <remarks></remarks>
Function FileZillaRecovery() As String
Dim XmlFilePath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\FileZilla\recentservers.xml"
Dim RecentServerList As New StringBuilder
Try
If File.Exists(XmlFilePath) Then
Dim FileZillaXmlDocument As New XmlDocument
FileZillaXmlDocument.Load(XmlFilePath)
Dim FileZillaNodeList As XmlNodeList = FileZillaXmlDocument.SelectNodes("//Server")
RecentServerList.AppendLine("FileZilla:")
For Each FileZillaXmlElement As XmlElement In FileZillaNodeList
RecentServerList.AppendLine(Environment.NewLine & "Host: " & FileZillaXmlElement.Item("Host").InnerText)
RecentServerList.AppendLine("Username: " & FileZillaXmlElement.Item("User").InnerText)
Dim HostPassword As String
If FileZillaXmlElement.Item("Pass").Attributes("encoding").InnerText = "base64" Then
HostPassword = Encoding.UTF8.GetString(Convert.FromBase64String(FileZillaXmlElement.Item("Pass").InnerText)) ' New Version (Base64 encoded)
Else
HostPassword = FileZillaXmlElement.Item("Pass").InnerText ' Old Version (Not Base64 encoded)
End If
RecentServerList.AppendLine("Password: " & HostPassword)
RecentServerList.AppendLine("Port: " & FileZillaXmlElement.Item("Port").InnerText)
Next
Return RecentServerList.ToString
Else
Return XmlFilePath & " was not found"
End If
Catch Ex As Exception ' You must specify the exception
Return Ex.Message
End Try
End Function
Fair usage:
Code:
MessageBox.Show(FileZillaRecovery()) ' Shows what that function returns