LINQ ile Klasördeki Dosyaları Listelemek

02.07.2009 09:35:06

Aşağıdaki kod yardımıyla LINQ kullanarak bir klasördeki istediğiniz uzantıya sahip dosyaları ListBox da listeleyebilirsiniz;

        private void GetList(string strPath)
        {
            DirectoryInfo path = new DirectoryInfo(strPath);
            try
            {
                if (path.Exists)
                {
                    var files = from file in path.GetFiles()
                                where file.Extension == ".jpg" || file.Extension == ".cmx"
                                select file.Name;
                    foreach (var fileName in files)
                        listKat.Items.Add(fileName);
                }
                else
                {
                    lblStat.Text = "Path not found !";
                }
            }
            catch (Exception exError)
            {
                lblStat.Text = "Error: " + exError.Message.ToString();
            }
        }