可以使用SSH.NET库来移动远程文件.可在此处获取:
https://github.com/sshnet/SSH.NET
以下是将第一个文件从一个源文件夹移动到另一个源文件夹的示例代根据FTP设置在类中设置私有变量.
using System;
using System.Linq;
using System.Collections.Generic;
using Renci.SshNet;
using Renci.SshNet.Sftp;
using System.IO;
using System.Configuration;
using System.IO.Compression;
public class RemoteFileOperations
{
private string ftpPathSrcFolder = "/Path/Source/";//path should end with /
private string ftpPathDestFolder = "/Path/Archive/";//path should end with /
private string ftpServerIP = "Target IP";
private int ftpPortNumber = 80;//change to appropriate port number
private string ftpUserID = "FTP USer Name";//
private string ftpPassword = "FTP Password";
///
/// Move first file from one source folder to another.
/// Note: Modify code and add a for loop to move all files of the folder
///
public void MoveFolderToArchive()
{
using (SftpClient sftp = new SftpClient(ftpServerIP, ftpPortNumber, ftpUserID, ftpPassword))
{
SftpFile eachRemoteFile = sftp.ListDirectory(ftpPathSrcFolder).First();//Get first file in the Directory
if(eachRemoteFile.IsRegularFile)//Move only file
{
bool eachFileExistsInArchive = CheckIfRemoteFileExists(sftp, ftpPathDestFolder, eachRemoteFile.Name);
//MoveTo will result in error if filename alredy exists in the target folder. Prevent that error by cheking if File name exists
string eachFileNameInArchive = eachRemoteFile.Name;
if (eachFileExistsInArchive)
{
eachFileNameInArchive = eachFileNameInArchive + "_" + DateTime.Now.ToString("MMddyyyy_HHmmss");//Change file name if the file already exists
}
eachRemoteFile.MoveTo(ftpPathDestFolder + eachFileNameInArchive);
}
}
}
///
/// Checks if Remote folder contains the given file name
///
public bool CheckIfRemoteFileExists(SftpClient sftpClient, string remoteFolderName, string remotefileName)
{
bool isFileExists = sftpClient
.ListDirectory(remoteFolderName)
.Any(
f => f.IsRegularFile &&
f.Name.ToLower() == remotefileName.ToLower()
);
return isFileExists;
}
}