C#如何在表单应用程序中按大小对文件排序 [英] C# How do I sort Files by Size in a Form Application

查看:83
本文介绍了C#如何在表单应用程序中按大小对文件排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我创建了一个Form App,该App可让您搜索目录和子目录.然后在文本框中显示选定的路径.然后,我提供了一个按钮,该按钮将显示路径并显示该路径中的所有文件以及文件大小.我要做的最后一步是按文件大小(从大到小)对文件进行排序.我发现了一些代码/方法,例如Array.Sort和IComparer,它们似乎可以针对控制台应用程序进行排序.

我需要表单应用程序的排序方法.

任何帮助,将不胜感激.

谢谢

Todd

Hello,

I have created a Form App that allows you to search a Directory and SubDirectory. Then displays the selected path in a textbox. I then provided a button that will take the path and display all files in that path and the file size. Last step I am trying to do is Sort the Files by their Size (Largest to Smallest). I have found some code/methods such as Array.Sort and IComparer which seem to sort for a Console Application.

I need the sort method for a form app.

Any help would be appreciated.

Thank You,

Todd

推荐答案

好吧,如果要实现IComparer,它可能会帮助您进行排序.

http://codebetter.com/blogs/david.hayden/archive/2005/03/06/56584.aspx [ ^ ]
Well, If you are going to implement IComparer it might help you to sort.

http://codebetter.com/blogs/david.hayden/archive/2005/03/06/56584.aspx[^]


我建​​议您使用Details模式在ListView中显示文件.这样,您可以轻松地按任何列(包括文件大小)进行排序.
不要急于投票.我不知道你的知识水平.如果发现使用ListViewListViewItem包含SubItemsListView困难,只需发表评论,我将尝试给您一些示例.
I suggest you to display the files in a ListView using Details mode. This way you could easily sort by any column, including the file size.
Don''t rush to downvote. I can not know your level of knowledge. If you find it difficult to work with a ListView with ListViewItems containing SubItems, just post a comment and I''ll try to give you some example.


foreach (string file in Directory.GetFiles(@"..."))
{
    ListViewItem item = new ListViewItem();
    item.Text = file;
    item.SubItems.Add(filesize.ToString());  // You get it somehow.
    item.SubItems.Add(someOtherString);  // Any other file property you like being displayed.
    listView1.Items.Add(item);
}


只需记住在设计器中将视图设置为Details并创建相应的Columns(也在设计器中).
现在,您可以按Columns的任意一个对ListView进行排序.
祝你好运!


Just remember to set the view to Details in the designer and to create the corresponding Columns (also in the designer).
Now you may sort the ListView by any of its Columns.
Good luck!


这篇关于C#如何在表单应用程序中按大小对文件排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆