question

QamarMo-8446 avatar image
1 Vote"
QamarMo-8446 asked MalamMalam-4042 commented

directoryInfo.CreationTime

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.

     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;
     }
dotnet-csharpdotnet-aspnet-webforms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @QamarMo-8446 ,
the code you provide already contains the result you expect:
DateTime creationTime = directoryInfo.CreationTime; and ListItem listItem = new ListItem(fileInfoTemp.Name, fileInfoTemp.Name);

So I'm not sure what you are looking for? What does need the date for something else mean? FileInfo.CreationTime?





0 Votes 0 ·

Again, here is my question...


How do I get the directoryInfo.CreationTime and the Filename in the ListBox (FileList) with Item Text and the Value.?
I want my listbox to display like this
-------------------------------------------------------
| 12-03-2020 File1.txt |
| 12-25-2019 MyFile1.pdf |
| 10-02-2019 AnotherFile1.pdf |
| 12-04-2020 MyFile1.pdf |
-------------------------------------------------------

0 Votes 0 ·

Please ignore my last post.

Your reponse ,made me realize what I was missing,

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

0 Votes 0 ·

0 Answers