private void buttonTestConnection_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(textBoxHost.Text) || (numericUpDownPort.Value < numericUpDownPort.Minimum || numericUpDownPort.Value > numericUpDownPort.Maximum) || string.IsNullOrEmpty(textBoxUsername.Text) || string.IsNullOrEmpty(textBoxPassword.Text)) { MessageBox.Show("Please make sure that at least the Host, Port, Username, and Password fields are correct.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } buttonTestConnection.Text = "Connecting ..."; buttonTestConnection.Enabled = false; Gavin.Kendall.SFTP.SftpClient sftpClient = new Gavin.Kendall.SFTP.SftpClient(textBoxHost.Text, (int)numericUpDownPort.Value, textBoxUsername.Text, textBoxPassword.Text); if (!sftpClient.Connect()) { MessageBox.Show("A connection could not be established with the file server.", "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); buttonTestConnection.Text = "Test Connection"; buttonTestConnection.Enabled = true; return; } if (sftpClient.IsConnected) { MessageBox.Show("A connection was successfully established with the file server.", "Connection Successful", MessageBoxButtons.OK, MessageBoxIcon.Information); } sftpClient.Disconnect(); buttonTestConnection.Text = "Test Connection"; buttonTestConnection.Enabled = true; } catch (Exception ex) { buttonTestConnection.Text = "Test Connection"; buttonTestConnection.Enabled = true; _log.WriteErrorMessage(ex.Message + "\n" + ex.StackTrace); MessageBox.Show("An error was encountered when attempting to establish a connection with the file server.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Uploads a screenshot to a file server. /// </summary> /// <param name="screenshot">The screenshot to upload.</param> /// <returns>True if the upload was successful otherwise false if the upload failed.</returns> private bool FileTransferScreenshot(Screenshot screenshot) { try { _log.WriteDebugMessage("Screenshot attempting to transfer to file server"); if (screenshot == null || string.IsNullOrEmpty(screenshot.Path)) { _log.WriteDebugMessage("Cannot upload screenshot to file server because screenshot is either null or path is empty"); return(false); } _log.WriteDebugMessage("Attempting to upload screenshot \"" + screenshot.Path + "\" to file server"); string host = Settings.SFTP.GetByKey("FileTransferServerHost", _config.Settings.DefaultSettings.FileTransferServerHost).Value.ToString(); _log.WriteDebugMessage("Host = " + host); int.TryParse(Settings.SFTP.GetByKey("FileTransferServerPort", _config.Settings.DefaultSettings.FileTransferServerPort).Value.ToString(), out int port); _log.WriteDebugMessage("Port = " + port); string username = Settings.SFTP.GetByKey("FileTransferClientUsername", _config.Settings.DefaultSettings.FileTransferClientUsername).Value.ToString(); _log.WriteDebugMessage("Username = "******"FileTransferClientPassword", _config.Settings.DefaultSettings.FileTransferClientPassword).Value.ToString(); if (string.IsNullOrEmpty(password)) { _log.WriteDebugMessage("Password = [empty]"); } else { _log.WriteDebugMessage("Password = [I'm not going to log this so check the user settings file]"); } if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { _log.WriteDebugMessage("Host, Username, or Password is empty"); return(false); } if (_sftpClient == null) { _sftpClient = new Gavin.Kendall.SFTP.SftpClient(host, port, username, password); } _log.WriteDebugMessage("Attempting to connect to file server"); if (!_sftpClient.IsConnected) { if (_sftpClient.Connect()) { _log.WriteDebugMessage("Connection to file server established"); } else { _log.WriteDebugMessage("Could not establish a connection with the file server"); return(false); } } // Make sure we are connected to the file server. If we were not connected earlier then a connection request would have been sent prior to this check. if (_sftpClient.IsConnected) { string destinationPath = System.IO.Path.GetFileName(screenshot.Path); _log.WriteDebugMessage("Attempting to upload screenshot to file server"); _log.WriteDebugMessage("Source: " + screenshot.Path); _log.WriteDebugMessage("Destination: " + destinationPath); if (_sftpClient.UploadFile(screenshot.Path, destinationPath)) { _log.WriteDebugMessage("Successfully uploaded screenshot"); } else { _log.WriteDebugMessage("Failed to upload screenshot"); return(false); } } return(true); } catch (Exception ex) { _screenCapture.ApplicationError = true; _log.WriteExceptionMessage("FormMain-Screenshots::FileTransferScreenshot", ex); return(false); } }