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;
}