0% found this document useful (0 votes)
87 views2 pages

Downloadfile

This C# code defines a class called Form1 that contains a button click event handler. The handler establishes an SFTP session using WinSCP to connect to a remote server, checks if a specific remote directory exists, then loops through files in that directory and downloads each one to a local backup folder. Any exceptions are written to the console.

Uploaded by

Prijesh Ramani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views2 pages

Downloadfile

This C# code defines a class called Form1 that contains a button click event handler. The handler establishes an SFTP session using WinSCP to connect to a remote server, checks if a specific remote directory exists, then loops through files in that directory and downloads each one to a local backup folder. Any exceptions are written to the console.

Uploaded by

Prijesh Ramani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinSCP;
using System.IO;

namespace WinScpTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
GiveUpSecurityAndAcceptAnySshHostKey = true,
Protocol = Protocol.Sftp,
HostName = "sftp.marexspectron.com",
UserName = "spectrometerEuropeS",
Password = "Yy7sJc",
//SshHostKeyFingerprint = "ssh-rsa 2048
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
};

using (Session session = new Session())


{
// Connect
session.Open(sessionOptions);
string remotePath = "/";

if (session.FileExists(remotePath))
{

RemoteDirectoryInfo directory =
session.ListDirectory(remotePath);

foreach (RemoteFileInfo fileInfo in directory.Files)


{
string remoteFilePath = "/" + fileInfo.Name;
string localPath = @"D:\BackUp\" + fileInfo.Name;
session.GetFiles(remoteFilePath, localPath).Check();
}

}
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex);

}
}

}
}

You might also like