Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

furieux

Apologies.
Since I'm very new to C#, it was my fault - I incorrectly used my code.
Now file uploads are working good and there is no new connection to SFTP server on each new file.
To explain what was wrong, it would take a lot of text here. There were multiple logic errors in my code.
martin

Re: Open session but don't upload files (C#)

furieux wrote:

And another method "Upload" which checks session.Opened and if yes, uploads file. But this approach doesn't work.

You didn't show us your code for this.
furieux

Open session but don't upload files (C#)

I'm using C# and I need such logic:
Default .NET FileSystemWatcher is watching for new files in the folder.
As soon as new file is found in folder (it is also being checked if not locked) I need to open connection to SFTP server and upload that file.

I created method "Connect" which is just opening connection to SFTP with session.Open(sessionOptions). And another method "Upload" which checks session.Opened and if yes, uploads file. But this approach doesn't work.
So how to accomplish this?

By the way if I use example from your site:
SessionOptions sessionOptions = new SessionOptions

            {
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx..."
            };
 
            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);
...................etc...etc...

Then this approach works, but if multiple files are being copied to the watching folder, WinSCP is creating SFTP connection on every file it uploads. I'm worried if that will be ok from SFTP server side.