The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

[Programming] C# List View / Filtering

cmsamocmsamo Registered User regular
edited February 2010 in Help / Advice Forum
I am working on a little home project to display images and videos in a list view from a particular directory on my PC. What I wanted to do is implement filters so that I could pick a start date, and end date, and a particular camera name, and filter the files that are displayed based on those choices. This isn't homework, its my own personal project to manage data from web-cameras...

Below is the function I use to fill my list views with data. At the moment, it works if I set pNameFilter as "*.*" and pass it in to the function.
private void PaintListView(System.IO.DirectoryInfo root, ListView lwFilesAndFolders, string pNameFilter)
        {
            try
            {
                ListViewItem lvi;
                ListViewItem.ListViewSubItem lvsi;
                List<FileInfo> lFi = null;
                string lFilter = null; // local string to make a filter for server name when looking for files to list...

                Console.WriteLine("\n\nPaintListView running...! \n");

                lwFilesAndFolders.Items.Clear();
                lwFilesAndFolders.BeginUpdate();
                
                // if no filter is passed in... we need to find everything (*.*)
                // if a filter is passed in, we find only folders/files with that exact string

                lFilter = pNameFilter; // XXX MAIN FILE FILTER HERE..

                System.IO.FileInfo[] files = null;
                System.IO.DirectoryInfo[] subDirs = null;

                // First, process all the files directly under this folder
                try
                {
                    files = root.GetFiles(lFilter);
                    
                    lFi = new List<FileInfo>();
                    //Add the files of the directory to the list
                    lFi.AddRange(files);
                    //Find the files on the list using a delegate
                    lFi = lFi.FindAll(delegate(FileInfo f) { return f.Extension.ToLower() == ".asf" || f.Extension.ToLower() == ".bmp"; }); 
                }
                // This is thrown if even one of the files requires permissions greater
                // than the application provides.
                catch (UnauthorizedAccessException e)
                {
                    // This code just writes out the message and continues to recurse.
                    // You may decide to do something different here. For example, you
                    // can try to elevate your privileges and access the file again.
                    //log.Add(e.Message);
                }

                catch (System.IO.DirectoryNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                }

                if (files != null)
                {
                    foreach (System.IO.FileInfo fi in lFi)
                    {
                        // In this example, we only access the existing FileInfo object. If we
                        // want to open, delete or modify the file, then
                        // a try-catch block is required here to handle the case
                        // where the file has been deleted since the call to TraverseTree().
                        Console.WriteLine("File Found: " + fi.FullName);
                        lvi = new ListViewItem();
                        lvi.Text = fi.Name;
                        lvi.ImageIndex = 1;
                        lvi.Tag = fi.FullName;

                        lvsi = new ListViewItem.ListViewSubItem();
                        lvsi.Text = fi.Length.ToString();
                        lvi.SubItems.Add(lvsi);

                        lvsi = new ListViewItem.ListViewSubItem();
                        lvsi.Text = fi.CreationTime.ToString();
                        lvi.SubItems.Add(lvsi);

                        lwFilesAndFolders.Items.Add(lvi);
                    }

                    // Now find all the subdirectories under this directory.
                    subDirs = root.GetDirectories();

                    foreach (System.IO.DirectoryInfo dirInfo in subDirs)
                    {
                        // Resursive call for each subdirectory.
                        Console.WriteLine("Searching Directory: " + dirInfo.FullName);
                        PaintListView(dirInfo,lwFilesAndFolders,lFilter);
                    }

                    lwFilesAndFolders.EndUpdate();
                }            

            }
My recorded video and images all contain the video they were made on in the filename. What I want to do is set the filter to the name of that file (populated from drop down box), and have the listview only be populated by items with that exact string as part of their filename...

I'm having problems writing the code to use the filter if one has been passed in, but use *.* if no filter was passed (i.e. on startup ALL files that are either .asp or .bmp are shown). The function is recursive to make sure it gets all files and folders below the initial directory.. which is confusing matters.

For the date part of my problem, I was going to get input from two datetimepickers and look at the file's creation time to make sure it's between the two dates... I think I can do that part just fine, once I sort out the filtering on filename.

Anyone got any ideas?

steam_sig.png
cmsamo on

Posts

  • TejsTejs Registered User regular
    edited February 2010
    '*' acts as a wildcard when matching to file names, so you should be able to do *text*.* and have it match to any file names that have 'text' somewhere in the file name, regardless of extension. If you want hard coded values, simply remove the '*' - so have *text*.bmp or so.

    Example:
    string listViewSelection = myDropDownBox.SelectedValue;
    
    string[] fileNamesFiltered = Directory.GetFiles(@"C:\PATH_NAME", "*" + listViewSelection + "*.*");
    

    Tejs on
Sign In or Register to comment.