Click here to Skip to main content
15,909,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to retrieve any one image from Folder, just by giving the Folder path.
How can I do it?
Posted
Comments
[no name] 10-Nov-11 11:45am    
What do you mean by retrieve? You want image name?
JawadHafiz 10-Nov-11 12:12pm    
i want any one image from folder

Based on your comment to JSOP's answer: try

string[] filenames = Directory.GetFiles(path);
Image image = Image.FromFile(filenames[0]);


You might want to sort the returned list first, as the order in which you get files is either undefined or defined in a complicated way that basically means you can't rely on a sensible order.
 
Share this answer
 
Comments
JawadHafiz 10-Nov-11 13:26pm    
it is taking root path of local
C#
string[] files = Directory.GetFiles(folderPath, "*.jpg", SearchOption.AllDirectories);
 
Share this answer
 
Comments
JawadHafiz 11-Nov-11 4:54am    
how to give folder path here, it is error!
Server Error in '/' Application.

Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\LeadImage\Pending'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\LeadImage\Pending'.

Source Error:


Line 29: // string[] filenames = Directory.GetFiles("LeadImage/Pending/");
Line 30:
Line 31: string[] files = Directory.GetFiles("LeadImage/Pending/", "*.jpg", SearchOption.AllDirectories);
Line 32: Imglead.ImageUrl = files.ToString();
Line 33:
JawadHafiz 12-Nov-11 13:40pm    
how to use its path on server?
C#
string[] files = Directory.GetFiles(@"D:\LeadImage\Pending\", "*.jpg", SearchOption.AllDirectories);


in your case you are looking for files in folder
'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\LeadImage\Pending'
Is it right?

and you can use symbol @ before string row. This provide you string like
C#
"D:\sdfs\sdfsd"


also you can check does current folder Exist:

C#
if (Directory.Exist(@"d:\Myimage\"))
{
}
 
Share this answer
 
Comments
JawadHafiz 11-Nov-11 12:07pm    
I got get that images from folder, how to get single img

Imglead.ImageUrl = files[0];
please help
C#
string[] files = Directory.GetFiles(@"D:\Photo\", "*.jpg", SearchOption.AllDirectories);


this row can get for you only array of file names, for example:

C#
files[0] == @"D:\Photo\1.jpg";
files[1] == @"D:\Photo\aaa.jpg";
files[2] == @"D:\Photo\MyPhoto.jpg";


And you row
C#
files.ToString();


will give you only name of type "files"
 
Share this answer
 
v2
Image image = Image.FromFile("put your file name here");


Specifying just the path (without the actual filename) could lead to problems if there are multiple images in the folder. Sure, you could juyst load the first image found, but unless you wrote extra code to provide a list of names in some sort of order (or by file type), you're always going to get the first file that was actualy written to the folder in question.

I guess what I'm trying to say is that we have no idea how to more accurately help you because your question is too vague.
 
Share this answer
 
Comments
JawadHafiz 10-Nov-11 12:08pm    
how to get the first file from folder?

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



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