C#如何在表单应用程序中按大小对文件排序 [英] C# How do I sort Files by Size in a Form Application
问题描述
你好,
我创建了一个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
中显示文件.这样,您可以轻松地按任何列(包括文件大小)进行排序.
不要急于投票.我不知道你的知识水平.如果发现使用ListView
和ListViewItem
包含SubItems
的ListView
困难,只需发表评论,我将尝试给您一些示例.
I suggest you to display the files in aListView
usingDetails
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 aListView
withListViewItem
s containingSubItems
, 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屋!