locked
A Listview with Filename and Date from File I/O RRS feed

  • Question

  • User1769015664 posted

    How do I get the directoryInfo.CreationTime and the Filename in the ListBox (FileList) with Item Text and the Value. I want to display the filename and need the date for something else.

    I want listbox to display items like this. I can get the datetime and file name but do not know how to add them to the Listbox.
    -------------------------------------------------------
    | 12-03-2020                  File1.txt |
    | 12-25-2019            MyFile1.pdf |
    | 10-02-2019    AnotherFile1.pdf |
    | 12-04-2020            MyFile1.pdf |
    -------------------------------------------------------

    private void BindFileNamesToList()
         {
             string extension = "pdf";  
             string folderPath = Server.MapPath("~/Files/");
             FileInfo[] fileInfo = GetFilesFromFolder(folderPath, (extension == "") ? "pdf" : extension);
             FilesList.Items.Clear();
             foreach (FileInfo fileInfoTemp in fileInfo)
             {
                 ListItem listItem = new ListItem(fileInfoTemp.Name, fileInfoTemp.Name);
                 FilesList.Items.Add(listItem);
             }
         }
         FileInfo[] GetFilesFromFolder(string folderName, string extension)
         {
             DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
             DateTime creationTime = directoryInfo.CreationTime;
             string internalExtension = string.Concat("*.", extension);
             FileInfo[] fileInfo = directoryInfo.GetFiles(internalExtension, SearchOption.AllDirectories);
                
             return fileInfo;
         }

    Friday, December 4, 2020 7:46 PM

Answers

  • User1769015664 posted

    Never mind, I have figured it out.

    I was missing the .ToString to convert date/time to a string. modified the line 

    ListItem listItem = new ListItem(file.CreationTime, file.Name);

    AS

    ListItem listItem = new ListItem(file.CreationTime.ToString("d").ToString(), file.Name);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2020 8:18 PM