---
title: "How to search for only jpg files in a directory using c#?"  
description: "How to search for only jpg files in a directory using c#?"  
author: "Anonymous User"  
published: 2015-03-17  
updated: 2015-03-17  
canonical: https://www.mindstick.com/forum/23022/how-to-search-for-only-jpg-files-in-a-directory-using-c-sharp  
category: ".net"  
tags: ["c#", "winforms"]  
reading_time: 1 minute  

---

# How to search for only jpg files in a directory using c#?

I want to [search](https://www.mindstick.com/articles/65368/best-smo-services-company-in-hyderabad-improve-search-rankings) a [directory](https://www.mindstick.com/forum/226/how-to-get-application-directory-using-c-sharp-csharp) and get the list of all jpg files. The [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that I have is as follow:

[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)[] fileList = Directory.Exists(this._imageDirectory)

? Directory.GetFiles(this._imageDirectory, "*.jpg")

: [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp);

This is working well if I have files such as below:

1.jpg

2.jpg

3.txt

In the above case it finds only [two files](https://www.mindstick.com/interview/2597/can-you-have-two-files-with-the-same-file-name-in-gac). But if I have the following files:

1.jpg

2.jpg

3.jpg_tmp

it finds 3 files. It finds 3.jpg_tmp which it should not find it.

How can I [fix](https://yourviews.mindstick.com/view/80763/donald-trump-ki-jeet-fix-hai) it without looking into all fileList and finding the ones that are not correct?

## Replies

### Reply by Anonymous User

You can filter the list with LINQ:

var pictures = fileList != null ?

fileList.Where(name => name.EndsWith(".jpg")).ToArray() :

Enumerable.Empty<string>().ToArray();


---

Original Source: https://www.mindstick.com/forum/23022/how-to-search-for-only-jpg-files-in-a-directory-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
