Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I have a folder with some files in it. The file names have a prefix (ID). Now i want to list all files with the same prefix in a web form.. Say i have an ID (22) and i want to retrieve all files with prefix 22 and list them. The Prefix is added when the files are uploaded... Can anyone help me out
Posted
Updated 12-Jul-12 0:13am
v2
Comments
Vani Kulkarni 12-Jul-12 6:11am    
I am assuming that listing means you want to display on a win form or webform? Let me know if my understanding is wrong.
Arjun Menon U.K 12-Jul-12 6:13am    
web form

C#
string searchPattern="22*";
public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
{ 
        return System.IO.Directory.GetFiles(path, searchPattern, searchOption);
}
 
Share this answer
 
C#
var d = new DirectoryInfo("PATH-TO-YOUR-FILES");
var files = d.GetFiles().Where(f => f.Name.StartsWith("11"));


You can add more filter if you want. By adding
.OrderByDescending(f=>f.CreationTime)
I will get newest files on top. And if I don't want all files I can simple add
.Take(10)
to get the 10 newest files and so on...
 
Share this answer
 
v2
Comments
Vani Kulkarni 12-Jul-12 6:25am    
Correct! My 5!
Shemeer NS 12-Jul-12 6:29am    
5'ed
Rahul Rajat Singh 12-Jul-12 6:30am    
Thats perfect. +5.
Arjun Menon U.K 12-Jul-12 6:35am    
Its not an ASPX page.. Using C# + HTML
StianSandberg 12-Jul-12 6:38am    
files will contain all files matching the query (in my example StartsWith("11")) You can then use files (an array of FileInfo) as a datasource for a repeater or datagrid or return data as json, xml or anything else basically.

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