C# Question - Listbox from String

Status
Not open for further replies.

EvilAlchemist

2[H]4U
Joined
Jan 11, 2008
Messages
2,730
Hey Guys,

I am totally blanking on how to get this code working.

I am trying to list the files in the directory --> List the last 10 Created --> Open User Selected file.

I need some help with the last 10 created part and opening the user selected file

I have the files listing .. but not in the correct order or limiting it to the last ten files

Code:
var pdfFiles = new DirectoryInfo("\\\\MPPDSERVER\\Forms\\CAD Reports").GetFiles("*.pdf");

            for (int i = 0; i < pdfFiles.Length; i++)
            {
                CADList.Items.Add(pdfFiles[i]);
            } 
        }
 
Last edited:
.GetFiles returns an IEnumerable.

I don't have an IDE so this code may not be right but you just need to say

pdfFiles.OrderByDescending(x=>x.CreationTimeUTC).Take(10)
 
Okay, So I made some serious progress on other parts of this I needed.

Still Missing the Sort by Date (Last one created first) and Last 10 only show but here is what I have so Far.

Code:
public partial class CAD : Form
    {
        public CAD()
        {
            InitializeComponent();
            var pdfFiles = new DirectoryInfo("\\\\MPPDSERVER\\Forms\\CAD Reports").GetFiles("*.pdf");
            for (int i = 0; i < pdfFiles.Length; i++)
            {
                CADList.Items.Add(pdfFiles[i]);
            } 
        }

        private void CADList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void OPEN_Click(object sender, EventArgs e)
        {
            string Server = "\\\\MPPDSERVER\\Forms\\CAD Reports\\";
            string CaseNumber = CADList.GetItemText(CADList.SelectedItem);
            Server += CaseNumber;
            System.Diagnostics.Process.Start(@Server);
            
        }
    }

If I use the following code, the List box does not populate correctly??
Code:
var d = new DirectoryInfo("\\\\MPPDSERVER\\Forms\\CAD Reports");
            var pdfFiles = d.GetFiles().OrderByDescending(f => f.CreationTime).Take(10);
 
Last edited:
Okay, so I figured out a little more ....

I got this issue solved .. thanks for all the help guys!
 
Last edited:
Status
Not open for further replies.
Back
Top