Trying to order a list of files that are sitting in a folder. I am able to display the files just fine and the links work but cannot order them by the file name while removing the last 4 characters (.xlsx). It's ordered by the first 2 digits and I need to remove the last 4 characters ".xls" from the end of the file name. The list of files are: 01-07-2022.xlsx 12-03-2021.xlsx 11-05-2021.xlsx etc.
I need them in that order and removing the .xlsx as it shows that name on the page. Right now it displays incorrectly below as:
12-04-2020.xlsx 12-03-2021.xlsx 11-07-2020.xlsx 11-05-2021.xlsx 10-01-2020.xlsx
just reading the first 2 digits for 12, 11, 10...etc. and then it shows a mix of the years.
My code to remove the characters and display in order. Design View:
<div id="pccommlist" runat="server" visible="true">
<asp:DataList ID="PCDataList" runat="server" RepeatColumns="1" BorderWidth="0" CellPadding="3" ForeColor="#2ba6cb"
Width="100%">
<FooterStyle BackColor="#ffffff" />
<ItemTemplate>
<asp:ImageButton Width="16px" ID="excelimage" runat="server" BorderStyle="None" ImageUrl='../imagessecure/icons/lists/excel.png' Height="16px" />
Precinct as of <asp:Label ID="lblDate" runat="server" />
<asp:HyperLink runat="server" NavigateUrl='<%#"pcredirect.aspx?name=" + DataBinder.Eval(Container.DataItem, "Name").ToString()%>' ID="pcLink" Target="_blank"
Text='<%# DataBinder.Eval(Container.DataItem, "Name").ToString() %>'>
</asp:HyperLink>
</ItemTemplate>
<FooterStyle BackColor="White" ForeColor="#2ba6cb" />
<ItemStyle BorderColor="White" BorderStyle="None" BorderWidth="0" HorizontalAlign="Center"
VerticalAlign="Bottom" BackColor="White" ForeColor="#2ba6cb" />
</asp:DataList>
</div>
Code Behind. Was trying to order it by creation time which was ideal and then tried full name as you see the commented out code above the FullName line.
private void GetXLS()
{
DirectoryInfo dir = new DirectoryInfo(MapPath("~/pcselect2022/"));
//FileInfo[] fileArray = dir.GetFiles("*.xlsx").OrderByDescending(p => p.CreationTimeUtc).ToArray(); //pull ONLY the xlsx files
FileInfo[] fileArray = dir.GetFiles("*.xlsx").OrderByDescending(p => p.FullName).ToArray(); //pull ONLY the xlsx files
char[] trimChars = { '.', 'x', 'l', 's' };
string fileName = fileArray.ToString().TrimEnd(trimChars);
//decide if we need to make controls visible
pccommlist.Visible = fileArray.Length > 0;
PCDataList.DataSource = fileArray;
PCDataList.DataBind();
}