string host = @"ftphost";
string username = "user";
string password = "********";
string localFileName = System.IO.Path.GetFileName(@"localfilename");
string remoteDirectory = "/export/";
using (var sftp = new SftpClient(host, username, password))
{
sftp.Connect();
var files = sftp.ListDirectory(remoteDirectory);
foreach (var file in files)
{
if (!file.Name.StartsWith("."))
{
string remoteFileName = file.Name;
if (file.LastWriteTime.Date == DateTime.Today)
Console.WriteLine(file.FullName);
File.OpenWrite(localFileName);
string sDir = @"localpath";
Stream file1 = File.OpenRead(remoteDirectory + file.Name);
sftp.DownloadFile(remoteDirectory, file1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用SSH.NET(Renci.SshNet)库来使用SFTP服务器.我需要做的是根据今天的日期从SFTP服务器上的特定文件夹中获取文件.然后将这些文件从SFTP服务器复制到我的服务器的本地驱动器.
以上是我的代码,但它不起作用.有时它说文件不存在但有时我将下载的文件不会在我的本地服务器上,但我需要下载当天上传到远程文件夹的任何文件.
有人可以看看,看看有什么问题吗?我认为它与流部分有关.除了上传文件之外,我还使用过FTP,我之前使用了一些以前的代码,并认为我可以改变这个过程,但这样做无效.我使用的代码基于这个例子.
我想在我的网站上显示下载过程的进度ProgressBar.我尝试过这样的代码来上传,但我失败了.这是我尝试失败的一个例子
private void button5_Click(object sender, EventArgs e)
{
Task.Run(() => Download());
}
private void Download()
{
try
{
int Port = (int)numericUpDown1.Value;
string Host = comboBox1.Text;
string Username = textBox3.Text;
string Password = textBox4.Text;
string SourcePath = textBox5.Text;
string RemotePath = textBox6.Text;
string FileName = textBox7.Text;
using (var file = File.OpenWrite(SourcePath + FileName))
using (var Stream = new FileStream(SourcePath + FileName, FileMode.Open))
using (var Client = new SftpClient(Host, Port, Username, Password))
{
Client.Connect();
progressBar1.Invoke((MethodInvoker)
delegate
{ …Run Code Online (Sandbox Code Playgroud)