Dev2.Data.PathOperations.Dev2FTPProvider.BuildSftpClient C# (CSharp) Method

BuildSftpClient() private method

private BuildSftpClient ( IActivityIOPath path ) : SftpClient
path IActivityIOPath
return Renci.SshNet.SftpClient
        SftpClient BuildSftpClient(IActivityIOPath path)
        {

            var hostName = ExtractHostNameFromPath(path.Path);
            if(hostName.ToLower().StartsWith("localhost"))
                hostName = hostName.Replace("localhost", "127.0.0.1");
            var methods = new List<AuthenticationMethod>();
            methods.Add(new PasswordAuthenticationMethod(path.Username, path.Password));
            
            if(!string.IsNullOrEmpty(path.PrivateKeyFile))
            {
                var keyFile = string.IsNullOrEmpty(path.Password) ? new PrivateKeyFile(path.PrivateKeyFile) : new PrivateKeyFile(path.PrivateKeyFile,path.Password);
                var keyFiles = new[] { keyFile };
                methods.Add(new PrivateKeyAuthenticationMethod(path.Username, keyFiles)); 
            }
            var con = new ConnectionInfo(hostName, 22, path.Username, methods.ToArray());
            var sftp = new SftpClient(con) { OperationTimeout = new TimeSpan(0, 0, 0, SftpTimeoutMilliseconds) };

            try
            {
                sftp.Connect();
            }
            catch(Exception e)
            {
                Dev2Logger.Log.Debug("Exception Creating SFTP Client");
                Dev2Logger.Log.Debug(e.Message);
                Dev2Logger.Log.Debug(e.StackTrace);
                {

                }
                if(e.Message.Contains("timeout"))
                {
                    throw new Exception("Connection timed out.");
                }
                if(e.Message.Contains("Auth failed"))
                {
                    throw new Exception(string.Format("Incorrect user name and password for {0}", path.Path));
                }
                if(path.Path.Contains("\\"))
                {
                    throw new Exception(string.Format("Bad format for SFTP. Path {0}. Please correct path.", path.Path));
                }
                throw new Exception(string.Format("Error connecting to SFTP location {0}.", path.Path));
            }
            return sftp;
        }