直接上代码如下:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
FtpHelper ftpHelper = new FtpHelper("xxxxx", "","Administrator","1qaz@qwer");
string[] datas = ftpHelper.GetAllList(null);
Console.WriteLine(JsonConvert.SerializeObject(datas));
Console.ReadLine();
}
}
public class FtpHelper
{
string ftpServerIP;
string ftpRemotePath;
string ftpUserID;
string ftpPassword;
string ftpURI;
/// <summary>
/// 连接FTP
/// </summary>
/// <param name="FtpServerIP">FTP连接地址</param>
/// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
/// <param name="FtpUserID">用户名</param>
/// <param name="FtpPassword">密码</param>
public FtpHelper(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
ftpServerIP = FtpServerIP;
ftpRemotePath = FtpRemotePath;
ftpUserID = FtpUserID;
ftpPassword = FtpPassword;
if (string.IsNullOrEmpty(ftpRemotePath))
{
ftpURI = "ftp://" + ftpServerIP + "/";
}
else
{
ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
}
}
/// <summary>
/// 下载
/// </summary>
/// <param name="filePath">下载保存的文件夹路径</param>
/// <param name="fileName">文件名称</param>
public bool Download(string filePath, string fileName)
{
try
{
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
reqFTP.Cred