---
title: "Get image file based on text box file name proplem"  
description: "Get image file based on text box file name proplem"  
author: "ahmed salah"  
published: 2014-06-07  
updated: 2014-06-10  
canonical: https://www.mindstick.com/forum/2023/get-image-file-based-on-text-box-file-name-proplem  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Get image file based on text box file name proplem

I have [windows](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer) form have 3 [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net)\
1-textbox1\
2-button\
3-[picture](https://answers.mindstick.com/qa/38554/name-the-oscar-winning-sound-designer-who-became-the-first-asian-to-win-the-award-for-best-sound-for-documentary-india-s-daughter-at-the-coveted-motion-picture-sound-editors-63rd-annual-golden-reel-awards) box1\
what i need is to [display image](https://www.mindstick.com/forum/33970/how-to-display-image-without-streach-in-uiscrollview) in picture box1 when i press button\
to show the [file name](https://www.mindstick.com/interview/2597/can-you-have-two-files-with-the-same-file-name-in-gac) that written in [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview) by searching the file in shared folder in [network](https://www.mindstick.com/articles/13122/an-introduction-to-network-cables) and if found it show it\
example\
I have shared folder"D:/images" have all permissions and is accessible found in [server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) 192.168.1.10 and it shared folder and have all pictures to all [employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks)\
What i need is when i write 111 in textbox1 and press button then it will go [search](https://www.mindstick.com/articles/65368/best-smo-services-company-in-hyderabad-improve-search-rankings) in folder images"D:/images" found in network in server 192.168.1.10 and search for file that have 111 image\
if it found show it in picture box1\
Are this possible?\
I need code by c# to make or achieve this task above.\
thanks\
\

## Replies

### Reply by ahmed salah

Thank you for reply\
if i need to make picture suitable for picture box\
what i do \
because half picture only display\
How to show full picture based on customized size

### Reply by Chris Anderson

hello ahmed,\
\
you can use the easy code given below:\
\

```
string path = Path.Combine("D:\\Images", textBox1.Text) + ".jpg";if (File.Exists(path))       pictureBox1.Image = Image.FromFile(Path.Combine("D:\\Images", textBox1.Text) + ".jpg");else{        pictureBox1.Image = null;        MessageBox.Show("File not exist");}
```

### Reply by ahmed salah

Thank you for reply\
i apply the code as above but remaining some points needed :\
using System;\
using System.Collections.Generic;\
using System.ComponentModel;\
using System.Data;\
using System.Drawing;\
using System.Linq;\
using System.Text;\
using System.Windows.Forms;\
using System.Data.SqlClient;\
using System.Drawing.Imaging;\
using System.IO;\
\
namespace FleetManagment\
{\
{\
FleetManagment.Fleet fleet2 = new FleetManagment.Fleet();\
private void button2_Click(object sender, EventArgs e)\
{\
try\
{\
string[] fileNames = Directory.GetFiles("D:\\Images");\
foreach (string file in fileNames)\
{\
\
if (file.Contains(textBox1.Text))\
{\
pictureBox1.Image = Image.FromFile(Path.Combine("D:\\Images", textBox1.Text) + ".jpg");\
}\
else\
{\
MessageBox.Show("File not exist");\
}\
}\
}\
catch (Exception ex)\
{\
MessageBox.Show(ex.ToString ());\
}\
\
}\
}\
}\
The code above is get picture from local machine and work successfully what i need is :\
1- get image from server 192.168.1.10 in folder path D:/images\
2-if folder images not have image written in textbox and press\
any file name wrong in textbox it give me exception\
how to prevent exception from show

### Reply by Chris Anderson

Hello ahmed,\
\
You can achieve this task by using following code:\
\
if (System.IO.File.Exists(Request.MapPath("~/images/" + textBox1.Text + ".jpg")))\
{\
//set the found image\
}\
else\
{\
//set the default image\
}\
\
System.IO.File.Exists(Request.MapPath("~/images/" + textBox1.Text + ".jpg"))\
The above line helps you to find image.\
\
I think this will help you otherwise you can post your reply if something found not working or missing.


---

Original Source: https://www.mindstick.com/forum/2023/get-image-file-based-on-text-box-file-name-proplem

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
