如何在C#中按整数对目录中的文件进行排序 [英] How to sort files in a directory by integer in C#
本文介绍了如何在C#中按整数对目录中的文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI
我想在包含.sql文件的目录中对文件进行排序,名为
1.sql
2.sql
3.sql
....
....
....
n.sql
我想按号码排序
我写了这段代码
string sqlConnectionString = cn.ConnectionString.ToString();
SqlConnection conn = new SqlConnection(sqlConnectionString);
string script = string .Empty;
服务器服务器= 新服务器( new ServerConnection(conn));
MessageBox.Show(server.Information.Version.ToString());
bool IsFailed = false ;
string mappath = Path.Combine(Application.StartupPath.ToString(), 脚本跨度>);
// var files1 =来自Directory.GetFiles(mappath)中的文件
// orderby file
// 选择文件;
// var largest = files1.First();
// var files = Directory.GetFiles(@ + mappath +,* .sql)。OrderBy(f => new FileInfo(f).Name);
var files = Directory.GetFiles( @ + mappath + , * .sql)。OrderBy(n = > n);
string [] filePaths = Directory.GetFiles( @ + mappath + , * .sql);
请你解决这个错误
解决方案
问题在你的 OrderBy(n => n),是alpha排序。
您需要将 n 转换为num值。
尝试排序类似 Convert.ToInt32(n)而不是 n
如果你的文件名只包含数值那么试试这个 -
var files = Directory.GetFiles( @ + mappath + , * .sql)。OrderBy(n = > Convert.ToInt16(Path.GetFileNameWithoutExtension(n)));
否则你可以根据这里分享的逻辑编写一个函数
http://stackoverflow.com/ a / 5093939/1006297 [ ^ ]
var files = Directory.GetFiles( @ + mappath + ,< span class =code-string> * .sql)。OrderBy(n = > PadNumericPortion(n));
希望,它有帮助:)
我写了这段代码现在我正确得到
var files = Directory.GetFiles( @ + mappath + , * .sql)。OrderBy( F => int.Parse(Path.GetFileNameWithoutExtension(F)));
HI
I want to sort files in a directory containing .sql files named as
1.sql
2.sql
3.sql
....
....
....
n.sql
I want to sort filed by number
I have written this code
string sqlConnectionString = cn.ConnectionString.ToString();
SqlConnection conn = new SqlConnection(sqlConnectionString);
string script = string.Empty;
Server server = new Server(new ServerConnection(conn));
MessageBox.Show(server.Information.Version.ToString());
bool IsFailed = false;
string mappath = Path.Combine(Application.StartupPath.ToString(), "Scripts");
//var files1 = from file in Directory.GetFiles(mappath)
// orderby file
// select file;
//var biggest = files1.First();
//var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(f => new FileInfo(f).Name);
var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => n);
string[] filePaths = Directory.GetFiles(@"" + mappath + "", "*.sql");
Would you please resolve this error
解决方案
The problem is in your OrderBy(n => n), is does an alpha sort.
You need to convert the n to the num value.
Try to sort on something like Convert.ToInt32(n) rather than on n
If your file names contains only numeric values then try this-
var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => Convert.ToInt16(Path.GetFileNameWithoutExtension(n)));
Else you can write a function based on the logic shared here
http://stackoverflow.com/a/5093939/1006297[^]
var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => PadNumericPortion(n));
Hope, it helps :)
I have written this code Now it is getting Correctly
var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(f=>int.Parse(Path.GetFileNameWithoutExtension(f)));
这篇关于如何在C#中按整数对目录中的文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文