Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: File upload failing - need help

$
0
0
The issue was I needed to append the intended file name on the remote directory like so:
public bool UploadFile(Settings settings, string localPath, string fileName)
        {
            try
            {
                using (var sftp = new SftpClient(settings.Host, settings.Username, settings.Password))
                {
                    sftp.Connect();
                    using (var fileStream = File.OpenRead(localPath))
                    {
                        var remotePath = settings.RemoteDirectory + fileName;
                        sftp.UploadFile(fileStream, remotePath, true);
                    }
                    sftp.Disconnect();
                }
                return true;
            }
            catch (Exception ex)
            {
                Utility.Err(ex, "UploadFile()");
                return false;
            }
        }
Now i just pass in the filename, and append it to the remote directory

The same is true for renaming files, both strings will need the full path and file name:
        public bool RenameRemoteFile(Settings settings, string oldFilePath, string newFilePath)
        {
            try
            {
                using (var sftp = new SftpClient(settings.Host, settings.Username, settings.Password))
                {
                    sftp.Connect();
                    sftp.RenameFile(oldFilePath, newFilePath);
                    sftp.Disconnect();
                }
                return true;
            }
            catch (Exception ex)
            {
                Utility.Err(ex, "RenameRemoteFile()");
                return false;
            }
        }

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>