ftp_get_file_and_directory
class DirectoryItem
{
public Uri BaseUri;
public string AbsolutePath
{ get { return string.Format("{0}/{1}", BaseUri, Name); } }
public DateTime DateCreated;
public bool IsDirectory;
public string Name;
public List<DirectoryItem> Items;
public override string ToString() { return Name; }
} internal enum enumFolderListFMT { UNIX, DOS_IIS };
internal enum enumFTPPlatform { WindowsServer2008, ServU}
/// <summary>
/// 获取目录信息(包含文件夹,文件)
/// </summary>
/// <param name="address"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
static List<DirectoryItem> GetDirectoryInformation(string addr2, string username, string password)
{
var address = addr2.EndsWith("/") ? addr2.Substring(, addr2.Length - ) : addr2;//去除最后一个斜杠
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false; string[] list = null;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
list = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
} //unix or dos_iis format?
enumFolderListFMT folderFormat = enumFolderListFMT.UNIX;
int dir_pos = ;
bool found = false;
foreach (var item in list)
{
if (item.ToLower().Contains("<dir>"))
{
folderFormat = enumFolderListFMT.DOS_IIS;
dir_pos = item.ToLower().IndexOf("<dir>");
found = true;
break;
}
}
if (!found && list.Length > && list[].ToLower()[] != 'd' && list[].ToLower()[] != '-')
{
folderFormat = enumFolderListFMT.DOS_IIS;
} enumFTPPlatform ftpPlatform = enumFTPPlatform.WindowsServer2008;
if (folderFormat == enumFolderListFMT.UNIX)
{
if (list.Length > && list[].Substring(, ).ToLower().Count(c => c == '-') < )
ftpPlatform = enumFTPPlatform.WindowsServer2008;
else
ftpPlatform = enumFTPPlatform.ServU;
} List<DirectoryItem> returnValue = new List<DirectoryItem>();
if (folderFormat == enumFolderListFMT.DOS_IIS)
{
foreach (var item in list)
{
if (item.ToLower().Contains("<dir>"))
{
var dir = item.Substring(dir_pos + ).Trim();
if (dir == "." || dir == "..") continue; var di = new DirectoryItem();
di.BaseUri = new Uri(address);
//di.DateCreated = dateTime;
di.IsDirectory = true;
di.Name = dir;
//Debug.WriteLine(di.AbsolutePath);
di.Items = GetDirectoryInformation(di.AbsolutePath, username, password);
returnValue.Add(di);
}
else
{
string filename = "";
if(found)
filename = item.Substring(dir_pos + ).Trim();
else
filename = item.Substring().Trim();
var di = new DirectoryItem();
di.BaseUri = new Uri(address);
di.IsDirectory = false;
di.Name = filename;
di.Items = null;
returnValue.Add(di);
}
}
}
else if (folderFormat == enumFolderListFMT.UNIX)
{
var pos = ftpPlatform == enumFTPPlatform.WindowsServer2008 ? : ;
foreach (var item in list)
{
if (item.Substring(, ).ToLower() == "d")
{
var dir = item.Substring(pos).Trim();
if (dir == "." || dir == "..") continue;
var di = new DirectoryItem();
di.BaseUri = new Uri(address);
di.IsDirectory = true;
di.Name = dir;
di.Items = GetDirectoryInformation(di.AbsolutePath, username, password);
returnValue.Add(di);
}
else if (item.Substring(, ).ToLower() == "-")
{
var filename = item.Substring(pos).Trim();
var di = new DirectoryItem();
di.BaseUri = new Uri(address);
di.IsDirectory = false;
di.Name = filename;
di.Items = null;
returnValue.Add(di);
}
}
}
return returnValue;
} /// <summary>
/// 下载文件
/// </summary>
/// <param name="filePath">下载到哪里</param>
/// <param name="outputFilename">下载后的文件名</param>
/// <param name="fileName">服务器上的文件名</param>
/// <param name="ftpServerIP">服务器全路径,注意最后的斜线不可少。如ftp://172.18.1.152:8009/aaa/</param>
/// <param name="ftpUserID">访问的用户名</param>
/// <param name="ftpPassword">访问的密码</param>
/// <returns></returns>
public int DownloadFile(string filePath, string outputFilename, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FtpWebRequest reqFTP;
try
{
//filePath = < <The full path where the file is to be created.>>,
//fileName = < <Name of the file to be created(Need not be the name of the file on FTP server).>>
FileStream outputStream = new FileStream(filePath + "\\" + outputFilename, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = ;
int readCount;
byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, , bufferSize);
while (readCount > )
{
outputStream.Write(buffer, , readCount);
readCount = ftpStream.Read(buffer, , bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return ;
}
catch (Exception ex)
{
// Logging.WriteError(ex.Message + ex.StackTrace);
System.Windows.Forms.MessageBox.Show(ex.Message);
return -;
}
}
get_directory_file_download
ftp_get_file_and_directory的更多相关文章
随机推荐
- R语言——绘图函数深入学习
利用R自带数据集 通过data()函数可以查看R自带数据集. > data() 返回以下结果,每一条记录都是一个数据,键入相应的数据名称可以查看具体信息. Data sets in packag ...
- MFC添加背景图片三种方法
方法一: 1.声明成员变量CBrush m_brush;2.在InitDialog中添加代码: ? CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP1); //IDB_B ...
- Chapter 1 First Sight——21
They were sitting in the corner of the cafeteria, as far away from where I sat as possible in the lo ...
- 【PDF单页转化为图片输出 注意:英文或图片类的PDF可转化,中文抛异常】
public static void main(String[] args) throws IOException { /** * PDF单页转化为图片输出 注意:英文或图片类的PDF可转化,中文抛异 ...
- 关于python的可变和不可变对象
在python中所有都是对象,在python中只有list和dict是可变对象,其他都是不可变对象. 具体参照:http://www.cnblogs.com/lovemo1314/archive/20 ...
- 关于NIOS ii烧写的几种方式(转)
源:http://www.cnblogs.com/bingoo/p/3450850.html 1. 方法一:.sof和.elf全部保存在FPGA内,程序加载和运行也是在FPGA内部. 把FPGA的配置 ...
- 用C#实现的两个试验编程(txt文本数据的导入,和数据导出为TXT)
1. 文件的存取 数据文件1.txt为学生成绩统计表,要对它做一个统计工作.文件中若一个人有多个成绩,则取他们的最好成绩来统计,然后计算全班平均成绩:统计0-69.70-79.80-89.90-100 ...
- (转)收集:Hibernate中常见问题 No row with the given identifier exists问题的原因及解决
Hibernate中No row with the given identifier exists问题的原因及解决 产生此问题的原因: 有两张表,table1和table2.产生此问题的原因就是tab ...
- 1.3 selenium IDE录制脚本转换为其他代码格式
1.在seleniumIDE录制框中点击Options->options... 在Enable experimental features前打对勾,点击确定.
- Linux Mint 17.2个性化配置
一.开启root 帐号登陆 设置一个口令,使用: sudo passwd root 当你使用完毕后屏蔽root帐号使用以下命令锁定root帐号 : sudo passwd -l root 如何在终端模 ...