in below file is showing from a directroy file but not sort out I need sort out by modified date.
please help me about this... thanks.
- private void Receipts_Load(object sender, EventArgs e)
- {
- try
- {
- string[] files = Directory.GetFiles(path);
- foreach (string file in files)
- {
- show_receipts_listbox.Items.Add(Path.GetFileName(file));
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message, "Error");
- }
- }

VulpesRank #55 · 29.2K reputationPosted Mar 19, 2015, 6:05 AM EDT
Try this:
private void Receipts_Load(object sender, EventArgs e)
{
try
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fia = di.GetFiles();
FileInfo t = null;
for (int p = 0; p <= fia.Length - 2; p++)
{
for (int i = 0; i <= fia.Length - 2; i++)
{
if (fia[i].LastWriteTime > fia[i + 1].LastWriteTime)
{
t = fia[i + 1];
fia[i + 1] = fia[i];
fia[i] = t;
}
}
}
foreach (FileInfo fi in fia)
{
show_receipts_listbox.Items.Add(fi.Name);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
VulpesRank #55 · 29.2K reputationPosted Mar 18, 2015, 3:14 PM EDT
using System.Linq;
You also need to be using .NET 3.5 (VS 2008) or later.
VulpesRank #55 · 29.2K reputationPosted Mar 18, 2015, 11:43 AM EDT
private void Receipts_Load(object sender, EventArgs e)
{
try
{
DirectoryInfo di = new DirectoryInfo(path);
string[] files = di.GetFiles()
.OrderBy(fi => fi.LastWriteTime)
.Select(fi => fi.FullName)
.ToArray();
foreach (string file in files)
{
show_receipts_listbox.Items.Add(Path.GetFileName(file));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
Feroz Khan69 reputationPosted Mar 18, 2015, 12:37 PM EDT
Feroz Khan69 reputationPosted Mar 18, 2015, 12:39 PM EDT
orderBY not accepting..
Feroz Khan69 reputationPosted Mar 18, 2015, 11:40 PM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 19, 2015, 6:18 AM EDT
Hi,
using Array.sort method, you can sort the array...
string[] files = Directory.GetFiles(path);
Array.Sort(files);
hope this will help you.
VulpesRank #55 · 29.2K reputationPosted Mar 19, 2015, 7:16 AM EDT
Feroz Khan69 reputationPosted Mar 20, 2015, 5:31 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 20, 2015, 7:12 AM EDT
Amar SrivastavaRank #1,375 · 1.2K reputationPosted Sep 19, 2017, 3:39 AM EDT
{
try
{
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
show_receipts_listbox.Items.Add(Path.GetFileName(file));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
Feroz Khan69 reputationPosted Mar 22, 2015, 11:09 AM EDT
now i want search field how can i search listbox value with textbox........
Feroz Khan69 reputationPosted Mar 22, 2015, 11:36 AM EDT