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

Advertisement

furieux
Joined:
Posts:
2

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.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,449
Location:
Prague, Czechia

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.

Reply with quote

furieux
Joined:
Posts:
2

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.

Reply with quote

Advertisement

You can post new topics in this forum