Get File Names of a Particular File Type using LINQ

This is a simple Trick to find File Names of a particular File Type existing in a Folder.


Suppose, you want to find the File Names of the CSV Files in a folder. So, you need to exclude all other files and consider the CSV Files only. This was implemented in one of my projects, where all the CSV File Names from a folder got populated in a DropDownList.

Let’s Explore

So, the main thing here is to find a particular type of File. For that, I used the following code.

DirectoryInfo di = new DirectoryInfo(folderPath);

// Get only the CSV files.
List csvFiles = di.GetFiles("*.csv")
                  .Where(file => file.Name.EndsWith(".csv"))
                  .Select(file => file.Name).ToList();

Here I have used DirectoryInfo.GetFiles Method (String).

Returns a file list from the current directory matching the given search pattern.

So, di.GetFiles("*.csv") would give us a list of all the CSV Files in that folder.

  • Here *.csv is the SearchPattern and * means any string before .csv.
  • Now, Where Clause is used to make sure that File Extension Ends With .csv and nothing else.

Then we are selecting the File Names by Select clause like Select(file => file.Name).


You can apply this Trick to find any File Type. You just need to change the SearchPattern accordingly. If you wish to find pdf files, then it would be di.GetFiles("*.pdf").

Full Code

I have used another Where condition here as per the requirements to exclude the CSV Files, having _something in their File Names.

/// This function Populates CSV File Names on a DropDownList.
private void PopulateCSVFilesDropDownList()
        string folderPath = GetFolderPath();

        if (!string.IsNullOrEmpty(folderPath))
            if (Directory.Exists(folderPath))
                DirectoryInfo di = new DirectoryInfo(folderPath);

                // Get only the CSV files excluding the ones having
                // "_something" appended to them.
                List csvFiles = di.GetFiles("*.csv")
                                  .Where(file => 
                                                 file.Name.EndsWith(".csv") &&
                                  .Select(file => file.Name).ToList();

                // Bind the DropDown and add one default option at the top.
                ddlCSVFiles.DataSource = csvFiles;
                ddlCSVFiles.Items.Insert(0, new ListItem("Please select a file", "-1"));
                // DropDownList is hided and Error message is displayed.
                ddlCSVFiles.Visible = false;
                lblErrorMessage.Visible = true;
                lblErrorMessage.Text = "Folder Does not Exist.";
    catch (Exception ex)
        // Exception is displayed on a Label.
        lblErrorMessage.Visible = true;
        lblErrorMessage.Text = ex.Message;

/// This function returns the Folder Path from web.config,
/// which contains different type of Files.
/// string: Folder Path
private string GetFolderPath()
    // For Example - D:\Projects\SomeProject\SomeFolder
    return (ConfigurationManager.AppSettings != null &&
            ConfigurationManager.AppSettings["FolderPath"] != null) ?
            ConfigurationManager.AppSettings["FolderPath"].ToString() :


27 Feb 2014 – Added an extra condition in LINQ to check if File Name Ends with .csv only and nothing else.


ASP.NET CheckBoxList Client Side Validation using JavaScript

ASP.NET CheckBox Validation Nothing Checked

ASP.NET CheckBox Validation Nothing Checked

ASP.NET CheckBox Validation Atleast one Checked

ASP.NET CheckBox Validation Atleast one Checked


In this Blog, we will explore a Trick to validate whether any CheckBox inside one CheckBoxList is checked or not.


When you define one CheckBoxList on aspx Page by writing code something like below…

<asp:CheckBoxList RepeatDirection="Horizontal" 
    <asp:ListItem Text="Apples" Value="1"></asp:ListItem>
    <asp:ListItem Text="Oranges" Value="2"></asp:ListItem>
    <asp:ListItem Text="Mangoes" Value="3"></asp:ListItem>

It will render on Browser like below…

<table id="chkDemo">
                <input type="checkbox" value="1" name="chkDemo$0" id="chkDemo_0">
                <label for="chkDemo_0">Apples</label>
                <input type="checkbox" value="2" name="chkDemo$1" id="chkDemo_1">
                <label for="chkDemo_1">Oranges</label>
                <input type="checkbox" value="3" name="chkDemo$2" id="chkDemo_2">
                <label for="chkDemo_2">Mangoes</label>

Basically it renders number of CheckBoxes depending on the number of ListItems inside CheckBoxList.

So, what is the Logic here?

We will call one JavaScript function on a Button Click.
Button would look like…

<asp:Button runat="server" ID="Button1" Text="Submit" 
            OnClientClick="return validateCheckBoxList();" />

Inside that function, we will run the following logic to validate whether any CheckBox is checked or not.

  1. We will find the main CheckBoxList first, which is rendered as a Table.
  2. Next, we need to find all the CheckBox inside that Table.
  3. After that, we have to check if any CheckBox is checked by looping through them.
  4. If any CheckBox is checked, then break the Loop and show alert (for demo purpose).
  5. Return true if any CheckBox is checked, else show alert and return false.
function validateCheckBoxList() {
    var isAnyCheckBoxChecked = false;

    // ::: Step-1 & 2 ::: Let's get all the CheckBoxes inside the CheckBoxList.
    var checkBoxes = document.getElementById("chkDemo").getElementsByTagName("input");
    // ::: NOTE ::: For jsfiddle demo I have directly used the ID. 
    // Otherwise you might have to use ClientID like below...
    // document.getElementById("<%= chkDemo.ClientID %>").getElementsByTagName("input");

    // ::: Step-3 ::: Now let's Loop through the Children.
    for (var i = 0; i < checkBoxes.length; i++) {
        if (checkBoxes[i].type == "checkbox") {
            if (checkBoxes[i].checked) {
                // ::: Step-4 ::: If current CheckBox is checked, then show alert.
                // Break the Loop.
                isAnyCheckBoxChecked = true;
                alert("Atleast one CheckBox is checked");
    // ::: Step-5 ::: Check if any CheckBox is checked or not.
    // Show alert and return accordingly.
    if (!isAnyCheckBoxChecked) {
        alert("No CheckBox is Checked.");

    return isAnyCheckBoxChecked;

See the Demo



I have used the CheckBoxList ID directly, which is chkDemo. But when your ClientID changes, you can get the CheckBoxList like…

document.getElementById("<%= chkDemo.ClientID %>");

Do you find it interesting?

Share your thoughts on the Blog. Don’t forget to Like and Share.



ASP :- Active Server Pages .NET :- .NET FrameworkASP.NET
According to Wiki

ASP.NET is a server-side Web application framework designed for Web development to produce dynamic Web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft’s Active Server Pages (ASP) technology.

So, simply, ASP.NET engine alters the aspx file (ASP.NET file extension is aspx), which contains some elements like TextBox, Label etc. and converts them to HTML control at the run time, before sending it to the user (browser).


ASP.NET was a successor to ASP. At initial stage of the release, ASP.NET was called ASP+.
Refer – ASP.NET (ASP+).

ASP.NET (originally called ASP+) is the next generation of Microsoft’s Active
Server Page
(ASP), a feature of their Internet Information Server (IIS).
Both ASP and ASP.NET allow a Web site builder to dynamically build Web pages on the fly by
inserting queries to a relational database in the Web page.


So, the idea was much transparent. ASP.NET was developed by introducing major enhancement to ASP. With the announcement of .NET Framework, ASP+ was renamed to ASP.NET, just to make it compatible with new .NET Nomenclature.

Do you have any other views?

Please share your thoughts. Don’t forget to Like and Share the Blog.