/// <summary> /// Method to send arbitrary files to server at target location /// </summary> /// <param name="hostPath">Location of file on PC</param> /// <param name="targetPath">Target location of file on server</param> /// <returns></returns> public Status SendFileContents(byte[] content, string targetPath) { /* This will only happen on second tab, so don't bother logging */ Status error = Status.Ok; SftpClient client = new SftpClient(_host.GetHostName(), "admin", ""); /* Try to connect to server */ if (error == Status.Ok) { client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, 6000); try { client.Connect(); } catch (Exception) { error = Status.CouldNotSFTPToServer; } } /* If connected to server, try to read the file */ if (error == Status.Ok) { if (content == null) { error = Status.CouldNotOpenFile; } /* If file has contents, delete and place it */ else { /* delete file first, regardless of success, continue */ try { if (client.Exists(targetPath)) { client.DeleteFile(targetPath); } } catch (Exception) { } /* write it */ try { client.WriteAllBytes(targetPath, content); } catch (Exception) { error = Status.CouldNotWriteFile; } } } return(error); }
public CtrSshClient(HostNameAndPort host, RioUpdater rioReference) { _rioReference = rioReference; _client = new SshClient(host.GetHostName(), "admin", ""); _client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, 6000); _client.Connect(); }
/// <summary> /// Method to send arbitrary files to server at target location /// </summary> /// <param name="hostPath">Location of file on PC</param> /// <param name="targetPath">Target location of file on server</param> /// <returns></returns> public Status SendFileContents(byte[] content, string targetPath) { /* This will only happen on second tab, so don't bother logging */ Status error = Status.Ok; SftpClient client = new SftpClient(_host.GetHostName(), "admin", ""); /* Try to connect to server */ if (error == Status.Ok) { client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, 6000); try { client.Connect(); } catch (Exception) { error = Status.CouldNotSFTPToServer; } } /* If connected to server, try to read the file */ if (error == Status.Ok) { if (content == null) { error = Status.CouldNotOpenFile; } /* If file has contents, delete and place it */ else { /* Create directory if missing, regardless of success, continue */ try { /* Create Directory if it does not exist (Remove File and only use directory) */ string temp = targetPath.Remove(targetPath.LastIndexOf('/')); client.CreateDirectory(temp); } catch (Exception) { /* Directory already exists */ } /* Delete file first, regardless of success, continue */ try { /* Don't need to perform check, always attempt delete */ if (client.Exists(targetPath)) { client.DeleteFile(targetPath); } } catch (Exception) { /* File could Not be found */ } /* Write file with its content */ try { client.WriteAllBytes(targetPath, content); } catch (Exception) { error = Status.CouldNotWriteFile; } } } return(error); }
public CtrSshClient(HostNameAndPort host, RioUpdater rioReference) { _rioReference = rioReference; _client = new SshClient(host.GetHostName(), "admin", ""); _client.Connect(); }