Saturday, August 13, 2011

Get the Files from specified Path

 Now we can easily get the file name in specific location of hard drive. here i have used Linq to get the file name from FileInfo[] collection.

use using System.IO; namespace for file related operations.
//This method return list of file names of given path
        public string getFilename(string path)
        {

            try
            {
                DirectoryInfo di = new DirectoryInfo(path);
                FileInfo[] files = di.GetFiles();

                //filter name of the files
                List lstfilenames = (from a in files
                                     select a.Name).ToList();


                return lstfilenames;
            }
            catch (Exception)
            {
                return lstfilenames;
            }
        }

Thanks for reading this article

No comments:

Post a Comment