按照特定的顺序得到一个文件夹里面的所有文件

Hywin1020 2009-10-15 11:13:03
得到一个文件夹里面的所有文件这我会。
我的文件一般都是这样命名的,1,2,3,4,5,6,7,8,9,10,11,12……45,……100.
直接得到的顺序是10,11,12,13……20,21,23……99.
我就想按照1234这样得到,有办法吗?

在windows下不要选自动排列刷新一下就好了,但是我要的是在C#中的结果。

怎样修改这段代码:

DirectoryInfo dir = new DirectoryInfo(oldPath);
int i=0;
foreach (FileInfo f in dir.GetFiles("*.txt"))
{
String name = f.Name;
nameList.Add(i,name);//nameList是一个哈希表
i++;
}
...全文
159 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
_see_you_again_ 2009-10-16
  • 打赏
  • 举报
回复
ArrayList list=new ArrayList();
list.Add(file);
list.Sort();
Hywin1020 2009-10-16
  • 打赏
  • 举报
回复
呃,大约可以了。
huming_h 2009-10-16
  • 打赏
  • 举报
回复
SortedList<string, string> list = new SortedList<string, string>();
list.Add("1", "11");
list.Add("0", "00");
list.Add("3", "33");
list.Add("2", "22");

for (int i = 0; i < list.Values.Count;i++ )
{
Response.Write(list.Keys[i]+":"+list.Values[i]);
Response.Write("<br/>");
}
huming_h 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hywin1020 的回复:]
引用 6 楼 huming_h 的回复:
用 SortedList替换你的hastable
  SortedList <string, string> list = new SortedList <string, string>();
            list.Add(name,name);

大哥,你这个怎么遍历?
[/Quote]

遍历根据keys和values都可以
传入key返回value
yanlongwuhui 2009-10-16
  • 打赏
  • 举报
回复
改成如下,调用方式不变:
private class ComparerFile : IComparer<string>
{
public int Compare(string x, string y)
{
string strX = null;
string strY = null;
double dblX = 0;
double dblY = 0;
int intPos = 0;
strX=System.IO.Path.GetFileNameWithoutExtension(x);
strY=System.IO.Path.GetFileNameWithoutExtension(y);
dblX = Conversion.Val(strX);
dblY = Conversion.Val(strY);
if (dblX == dblY)
{
return x.CompareTo(y);
}
else
{
return dblX.CompareTo(dblY);
}
}
}
yanlongwuhui 2009-10-16
  • 打赏
  • 举报
回复
参考如下:
引入Microsoft.VisualBasic命名空间
private class ComparerFile : IComparer<string>
{
public int Compare(string x, string y)
{
string strX = null;
string strY = null;
double dblX = 0;
double dblY = 0;
int intPos = 0;
strX=System.IO.Path.GetFileNameWithoutExtension(x);
strY=System.IO.Path.GetFileNameWithoutExtension(y);
dblX = Conversion.Val(strX);
dblY = Conversion.Val(strY);
intPos = strX.IndexOf(dblX.ToString());
strX = strX.Substring(intPos + 1);
intPos = strY.IndexOf(dblY.ToString());
strY = strY.Substring(intPos + 1);
if (dblX == dblY)
{
return strX.CompareTo(strY);
}
else
{
return dblX.CompareTo(dblY);
}
}
}

调用:
 string[] strFiles = null;
ComparerFile objComparer = new ComparerFile();
strFiles = System.IO.Directory.GetFiles(oldPath, "*.TXT");
Array.Sort(strFiles, objComparer);
robin521 2009-10-16
  • 打赏
  • 举报
回复
从根本上讲,看文件夹本身是不是有序的.
happyboyxq 2009-10-16
  • 打赏
  • 举报
回复
楼上的方法看起来很复杂的样子,LZ的文件都是1-**.txt嘛,用-做分隔符一查就可以排序了啊。
csszhz 2009-10-16
  • 打赏
  • 举报
回复
预定义好的方式不知道,我给写了一个。
只是数字前缀不可重复,要想重复就不能使用hashtable了。
有谁帮忙改一下也可以。

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

string[] str = new string[10];

str[0] = "7abcd";
str[1] = "4efgh";
str[2] = "3desw";
str[3] = "2ferf";
str[4] = "18uhyt";
str[5] = "5aiuy";
str[6] = "22bbby";
str[7] = "10rted";
str[8] = "9artd";
str[9] = "17uyij";

Console.WriteLine("排序前");
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine(str[i]);
}

Console.WriteLine("\n排序之后");

string[] str2 = getSortString(str);

for (int i = 0; i < str2.Length; i++)
{
Console.WriteLine(str2[i]);
}

Console.Read();
}
/// <summary>
/// 将传入数组中以数字开头的元素保留下来并按照数字排序
/// 开头的数字不可以有重复值,否则出现异常
/// </summary>
/// <param name="str"></param>
/// <returns>排序号的数组</returns>
private static string[] getSortString(string[] str)
{
int[] orderStr = new int[str.Length];
Hashtable list = new Hashtable();
string[] overString=new string [str.Length];

for (int i = 0; i < str.Length; i++)
{
string pattern = @"\b[0-9]{1,5}";
MatchCollection Matches = Regex.Matches(str[i], pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

foreach (Match var in Matches)
{
orderStr[i] = int.Parse(var.Value);
list.Add(var.Value, str[i]);
}
}

Array.Sort(orderStr);

for (int i = 0; i < orderStr.Length; i++)
{

if (orderStr[i] != 0)
{
overString[i]=list[orderStr[i].ToString()].ToString();
}

}

return overString;
}
}
}

zzxap 2009-10-16
  • 打赏
  • 举报
回复
改一下命名方式
Hywin1020 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lcl_data 的回复:]
DirectoryInfo dir = new DirectoryInfo(oldPath);
            int i=0;
            foreach (FileInfo f in dir.GetFiles("*.txt"))
            {
                String name = f.Name;
                nameList.Add(i,name);//nameList是一个哈希表  可不可以只取-前面的进入nameList,然后排序,你试试
                i++;
            }


[/Quote]
截取之后再排序多没有意思啊,windows能实现按说也差不多啊。
windows的文件,如果文件名的前缀是数字的话默认按10,11,……99这样排,但是将排序里的自动排序点去,就按1,2,3,4……这样排了。我就以为可能在程序里像那样直接得到。
如果不能,那就只好自己写一个排序喽。
Hywin1020 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 huming_h 的回复:]
用 SortedList替换你的hastable
SortedList <string, string> list = new SortedList <string, string>();
            list.Add(name,name);
[/Quote]
大哥,你这个怎么遍历?
十八道胡同 2009-10-15
  • 打赏
  • 举报
回复
DirectoryInfo dir = new DirectoryInfo(oldPath);
int i=0;
foreach (FileInfo f in dir.GetFiles("*.txt"))
{
String name = f.Name;
nameList.Add(i,name);//nameList是一个哈希表 可不可以只取-前面的进入nameList,然后排序,你试试
i++;
}

Hywin1020 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lcl_data 的回复:]
C# codeList<int> list=new List<int>();
list.Add(2);
list.Add(22);
list.Add(1);
list.Add(12);
list.Sort();foreach (intvarin list)
{
Console.WriteLine(var);
}
1
2
12
22
[/Quote]
我没说清楚,都是字符串

static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("2-hh.txt");
list.Add("20-hh.txt");
list.Add("12-hh.txt");
list.Add("1-hh.txt");
list.Sort();
foreach (string var in list)
{
Console.WriteLine(var);
}
Console.Read();
}


12-hh.txt
1-hh.txt
20-hh.txt
2-hh.txt

再说了,我不要这个啊,我就要直接得到文件目录后按顺序排。
要是自己写排序那很简单啊。
十八道胡同 2009-10-15
  • 打赏
  • 举报
回复
List<int> list = new List<int>();
list.Add(2);
list.Add(22);
list.Add(1);
list.Add(12);
list.Sort();
foreach (int var in list)
{
Console.WriteLine(var);
}

1
2
12
22
huming_h 2009-10-15
  • 打赏
  • 举报
回复
用 SortedList替换你的hastable
SortedList<string, string> list = new SortedList<string, string>();
list.Add(name,name);
Hywin1020 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huming_h 的回复:]
????没看明白意思
你的1,2,3,4呢?排在那里了?
[/Quote]
就是得到之后按数字前缀1,2,3,4……这样排列,不要按10-***.txt,11-***.txt……这样排列,程序默认的是后一种。
Hywin1020 2009-10-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 whycom 的回复:]
string[] str = dir.GetFiles("*.txt");
str.sort();
[/Quote]
呃,补充一点,文件名不是纯数字的,一般是1-****.txt,2-***.txt,3-***.txt…… 这样的,数组的排序不行吧?
huming_h 2009-10-15
  • 打赏
  • 举报
回复
????没看明白意思
你的1,2,3,4呢?排在那里了?
whycom 2009-10-15
  • 打赏
  • 举报
回复
string[] str = dir.GetFiles("*.txt");
str.sort();
加载更多回复(1)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧