Get all the file name from a given Directory including sub directories in asp.net


Description:-

In this article we will see about how to get files name from directory or subdirectory which we assign in directory. It’s simple to get all files name from the directory using the DirectoryInfo class and get the all files from that directory in dot.net. we have to set for Subdirectory if there are sub directory is there than it will not check for sub directory it will count from root directory only and return file name from that root directory. Here is the demo to count from directory or subdirectory also.

Default.aspx.cs:-

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    DirectoryInfo d = new DirectoryInfo(Server.MapPath("~/FileToRead/"));
    string d2 = d.FullName.ToString();
    List<string> Files = Directory.GetFiles(d2, "*", SearchOption.AllDirectories).ToList();
    foreach (List<string> li in Files)
    {
      Response.Write(Files[0].ToString());
    }
  }
}

Related Posts

Previous
Next Post »

Thanks for comments.....