做了一个从FTP上下载文件的工具、
FTP上的文件是这样的:
现在要做的就是选中一个文件夹,选出相同name文件中序号最大的文件。
其实就是选出这些文件中每一个的最新版本,做一个全部文件最新版本的集合。
然后删除其余的文件。
参考了许多方法。比如说dictionary、正则表达式、类型转换之后LIST..
最后还是用了最直接的方法。就是把name和序号分割,然后循环找出同名中最大序号的。
本来很简单的,但是由于每次要比较两个,遇到最后一个的时候就比较麻烦,又多写了一些。
毕竟我也是刚学C#,一定有更好的方法的。先记录下来
private void SelectNew_Click(object sender, EventArgs e)//排序
{
//string str;
// str.Split(' ')
string[] AllFiles = GetFilesList();//获得全部文件名
int Len = AllFiles.Length;
string newstr="";
string name = "1";
string[] a = new string[Len];
string[] b = new string[Len];
int i = 0,coo=0;
int i1 = 0, i2 = 0, Fir = 0, j = 0, k = 0, Total = 0, max=0;
int[] bnum;
int[] bcomp = new int[Len];
// comp = AllFiles.OrderBy(s => int.Parse(Regex.Match(s, @"\d+").Value)).ToArray();
for (i = 0; i < Len; i++)
{
AllFiles[i] = AllFiles[i].Substring(39);//AllFiles具有完整的名称
newstr = AllFiles[i];
newstr = newstr.Substring(0, newstr.Length - 4); //newstr去除.ZIP
coo =1+newstr.LastIndexOf("_");//检索最后一个"_"的位置
a[i] = newstr.Substring(0,coo); //MessageBox.Show(a[i]);//名字给A
b[i] = newstr.Substring(coo);// MessageBox.Show(b[i]);//数字给B
}
bnum = Array.ConvertAll(b, int.Parse);//iNums = Array.ConvertAll<string, int>(sNums , s => int.Parse(s)); iNums = Array.ConvertAll<string, int>(sNums, int.Parse);
// ArrayList listb = new ArrayList(bnum);
// MessageBox.Show(bnum[0].ToString ()); // listb.Sort();
// bnum = Array.ConvertAll<string, int>(b, s => int.Parse(s));
for (i1=0;i1<(Len-1);i1++ )//开始选出最高版本文件
{
name = a[i1];
if (a[i1]==a[i1+1])
{
Total++;
}
if ((!(a[i1] == a[i1 + 1]))||(i1==(Len-2)))//当有文件名字变更或者是到达文件末尾的时候
{
Array.Clear(bcomp, 0, a.Length);//清空这个bcomp数组
for (k=Fir,j=0;j<(Total+1) ;k++ ,j++)//将这一组数据给这个获取最大数的的数组赋值
{
bcomp[k]= bnum[k];
}
max= bcomp.Max();//选出最大的那个数字
name = name + max + ".zip";
Download1(SelectDir , @"D:\",name, pb1);//直接将名字和数字拼接在一起,下载
Total = 0;
Fir = i1+1;
}
i2 = i1 + 1; //MessageBox.Show(i2.ToString());
if ((i2 == (Len - 1)) && (a[i2] != a[i2 - 1]))//多判断一次,最后两个不同
{
name = a[i2] + bnum[i2] + ".zip";
Download1(SelectDir, @"D:\", name, pb1);
}
}
}