实现远程FTP特定时间轨道号MODIS数据的搜索

private ArrayList alst = new System.Collections.ArrayList();//建立ArrayList对象
int strLength = 0;
string dirSub = null;
private Ftp ftpClient;

public FormSearch()
{
InitializeComponent();
InitProduct("/allData/5/");
}

//产品类型初始化
private void InitProduct(string dir)
{
ftpClient = new Ftp("ftp://ladsweb.nascom.nasa.gov", "anonymous", "rufeixida@sina.com");
string[] remoteContents = ftpClient.directoryList(dir);
if (remoteContents != null)
{
foreach (string strRemote in remoteContents)
{
if (strRemote == "")
{
break;
}
else
{
if (strRemote.Contains("MOD") || strRemote.Contains("MYD"))
comboProduct.Items.Add(strRemote);
}
}
}
}
//递归实现搜索
private void GetFiles(string dir, Ftp ftpClient)
{
if (dir == "" || dir == null)
MessageBox.Show("文件搜索完成");
else
{
string[] remoteContents = ftpClient.directoryList(dir);
if (remoteContents != null)
{
foreach (string strRemote in remoteContents)
{
if (strRemote == "")
{
dirSub = dir.Substring(0, dir.Length - strLength);
break;
}
else
{
string strTime = null;
string exname = strRemote.Substring(strRemote.LastIndexOf(".") + 1);//得到后缀名 
if (strRemote.Length > 10)
{
strTime = strRemote.Substring(strRemote.IndexOf(".") + 2, 7);//得到时间
}
if (".hdf".IndexOf(strRemote.Substring(strRemote.LastIndexOf(".") + 1)) > -1 && strRemote.Contains(NumberTxtBox.Text) && checkTime(strTime, this.dateTimePicker1.Text, this.dateTimePicker2.Text))
{
alst.Add(strRemote);//把.hdf文件全名加人到ArrayList对象   
}
else
{
if (checkIsFile(dir + strRemote))
continue;
else
{
string strRemotes=strRemote + "/";
strLength = strRemotes.Length;
dir += strRemotes;
if (dirSub != null)
{
dirSub += strRemotes;
GetFiles(dirSub, ftpClient);
}
else
//if (strRemotes == comboProduct.SelectedText)
GetFiles(dir, ftpClient);
}
}
}
}
}
else
{
dirSub = dir.Substring(0, dir.Length - strLength);
}
}
}
//判断文件是否为空
private bool checkIsFile(string nodeText)
{
string ext = System.IO.Path.GetExtension(nodeText);

if (ext == "")
return false;
else
return true;
}
//搜索
private void ButtonSearch_Click(object sender, EventArgs e)
{
try
{
string ftpString = "/" + "allData/5/" + comboProduct.SelectedItem.ToString() + "/2013/";
this.GetFiles(ftpString, ftpClient);
string[] list = (string[])alst.ToArray(typeof(string));//把ArrayList转化为string[]  
ftpClient = null;
FormResult frm = new FormResult(list);
frm.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show("搜索错误. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//年月日转换
private string dataTimeToDays(string dateTimePicker)
{
string strTP = dateTimePicker;
char[] splits = new char[] {'年', '月', '日' };
string[] strTPs = strTP.Split(splits);
int days = Convert.ToInt32(strTPs[1]);
days *= 30;
int day = Convert.ToInt32(strTPs[2]);
day += days;
string reStr = strTPs[0] + day.ToString();
if (reStr.Length == 6)
{
reStr = reStr.Substring(5);
return strTPs[0] + "0" + reStr;
}
else if (reStr.Length == 5)
{
reStr = reStr.Substring(5);
return strTPs[0] + "00" + reStr;
}
else
return reStr;
}
//判断是否在选定时间间隔中
private bool checkTime(string nameTime, string dateTimePickerBegain, string dateTimePickerEnd)
{
string dateTimeBegain = dataTimeToDays(dateTimePickerBegain);
string dateTimeEnd = dataTimeToDays(dateTimePickerEnd);
if (Convert.ToInt32(nameTime) <= Convert.ToInt32(dateTimeEnd) && Convert.ToInt32(nameTime) >= Convert.ToInt32(dateTimeBegain))
return true;
else
return false;
}

原文地址:https://www.cnblogs.com/lxc-binary/p/3336938.html