Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my window application I change image of button when click. I save data in text file using SaveFileDialog. After save data in text file when I click my button it well generate error like this:"File not found Exception was caught". If file not found then I will not change image before save data in text file. I use this code at button click event for image change:
C#
btn_Stop.Image = Bitmap.FromFile("..\\Image\\btn_Stop.Image.jpg");
btn_Start.Image = Bitmap.FromFile("..\\Image\\btn_Start.red.jpg");
Posted
Updated 22-Feb-12 0:31am
v2

You really do not want to use a relative path here - it is just that: relative to the current location.

If you store these images in a directory below the location where the application resides, then specify that folder absolutely:
C#
btn_Stop.Image = Bitmap.FromFile(Application.StartupPath + "\\Image\\btn_Stop.Image.jpg");
btn_Start.Image = Bitmap.FromFile(Application.StartupPath + "\\Image\\btn_Start.red.jpg");
 
Share this answer
 
Comments
jaideepsinh 22-Feb-12 7:53am    
Thank's.
This one work perfect as i want.
OriginalGriff 22-Feb-12 8:19am    
You're welcome!
krumia 22-Feb-12 9:02am    
Adding to that: Doesn't "\\directory\\file.ext" means that the path is relative to the root of the current drive? If we want to have a relative path shouldn't we use ".\\directory\\file.ext"?
In addition to the Solution 1,

C#
btn_Stop.Image = Bitmap.FromFile(Path.Combine(Application.StartupPath , "Image\\btn_Stop.Image.jpg"));
btn_Start.Image = Bitmap.FromFile(Path.Combine(Application.StartupPath , "Image\\btn_Start.red.jpg"));


Hope it helps :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900