0

I created a Windows application to run on any Windows 10 OS with a button function which when the user click, will load all available .map files name from a certain folder. Upon loading the available file, it will display all the files in a ListView forms and which the files will be based on the files naming. However, how do I make it to sort according to the date of the file has been created/modified?

Here is my code :

  private void btnLot_Click(object sender, EventArgs e)
    {            
        string _ext = string.Empty;
        DialogResult result = openLotDialog.ShowDialog();

        if (result == System.Windows.Forms.DialogResult.Cancel)
        {
            return;
        }

        lvMapFiles.Items.Clear();

        m_IUpperPCBMacData = null;
        m_ILowerPCBMacData = null;

        picDevTop.Invalidate();

        if (System.IO.Path.GetExtension(m_FolderDialog.SelectedPath).Trim() != "")
        {
            MessageBox.Show(this, "Please select a folder. Don't select a file.", "Open Folder Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        string[] files = Directory.GetFiles(openLotDialog.SelectedPath);
        // Add these files into list view

        pathDirectory = openLotDialog.SelectedPath;
        //pathDirectory = m_FolderDialog.SelectedPath;
        fileExtension = string.Empty;

        int i = 0;

        foreach (string s in files)
        {
            fileName = System.IO.Path.GetFileNameWithoutExtension(s);
            _ext = System.IO.Path.GetExtension(s);

            if (fileName.Trim().ToUpper().Contains("_TEMP"))
            {
                System.IO.File.Delete(s);
                continue;
            }

            if (fileName.Trim().ToUpper().Contains("_SUMMARY"))
            {
                continue;
            }

            if (_ext.Trim().ToUpper() != ".MAP")
            {
                continue;
            }

            if (fileExtension.Trim() == "")
            {
                fileExtension = System.IO.Path.GetExtension(s);
            }
            
            ListViewItem _lvi1 = new ListViewItem();
            _lvi1.SubItems.Add("");

            lvMapFiles.Items.Add(_lvi1);
            lvMapFiles.Items[i].Text = fileName;

            i++;
        }

        //m_CalcParticle = true;
        CUtil.Instance.ResetDefectCodeAndDescription();

        if (ReadMapFiles(pathDirectory + "\\" + lvMapFiles.Items[0].Text + fileExtension))
        {
            lvMapFiles.Items[0].Selected = true;

            LoadDefectCodeList();
            LoadDefectMenuList();
        }
        else
        {
            lvMapFiles.Items.Clear();
            MessageBox.Show(this, "Fail to Open Map File. Make sure to select the correct Map file.", "Read Map Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }            
    }

Assuming the code below will read and add all available files to the ListView, is there any way I can add a function to sort the files first before adding to the ListView?

    foreach (string s in files)
        {
            fileName = System.IO.Path.GetFileNameWithoutExtension(s);
            _ext = System.IO.Path.GetExtension(s);

            if (fileName.Trim().ToUpper().Contains("_TEMP"))
            {
                System.IO.File.Delete(s);
                continue;
            }

            if (fileName.Trim().ToUpper().Contains("_SUMMARY"))
            {
                continue;
            }

            if (_ext.Trim().ToUpper() != ".MAP")
            {
                continue;
            }

            if (fileExtension.Trim() == "")
            {
                fileExtension = System.IO.Path.GetExtension(s);
            }
            
            ListViewItem _lvi1 = new ListViewItem();
            _lvi1.SubItems.Add("");

            lvMapFiles.Items.Add(_lvi1);
            lvMapFiles.Items[i].Text = fileName;

            i++;
        }

Thank you for the help!

Winter Bucky
  • 137
  • 3

1 Answers1

1

You can leverage the System.IO.DirectoryInfo type, as well as LINQ in order to apply sorting.

First include the appropriate namespaces, if they are not already present.

using System.Collections.Generic;
using System.IO;
using System.Linq;

Then update the files variable assignment to load the directory, sort by creation date (ascending).

IEnumerable<string> files = new DirectoryInfo(openLotDialog.SelectedPath)
  .GetFiles()
  .OrderBy(x => x.CreationTime)
  .Select(x => x.FullName);

Note that there may be other properties or methods available on System.IO.FileInfo that may be beneficial to use as opposed to reverting back to the full path.

joncloud
  • 757
  • 5
  • 14