Solved

How to pick only the filename from FileInfo

Posted on 2012-04-11
7
353 Views
Last Modified: 2012-04-11
I am getting the files from FileInfo class as follows:
DirectoryInfo dir = new DirectoryInfo(MapPath("Files/"));
        FileInfo[] files = dir.GetFiles(string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text));
        ArrayList listItems = new ArrayList();
        foreach (FileInfo info in files)
        {
            listItems.Add(info);
            Response.Write(info.ToString() + "<BR>");
        }
        GridView1.DataSource = files;
        GridView1.DataBind();

But when i assign it to gridview, it lists all the information of the file.
How to pick only the filename.
0
Comment
Question by:searchsanjaysharma
  • 3
  • 3
7 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
Comment Utility
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
Rather than using DirectoryInfo, you can use

List<string> lstFiles = Directory.GetFiles("Path",string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text),SearchOptions.AllDirectories).ToList()
GridView1.DataSource = lstFiles;
GridView1.DataBind();

Open in new window


This will return you an array of filenames...

Otherwise here is fileinfo way:-
foreach (FileInfo info in files)
        {
            listItems.Add(info.Name);
            Response.Write(info.ToString() + "<BR>");
        }

Open in new window

0
 

Author Comment

by:searchsanjaysharma
Comment Utility
Not working, giving error in searchoptions.
the name 'SearchOptions' does not exist in the current context
List<string> lstFiles = Directory.GetFiles("Path",string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text),SearchOptions.AllDirectories).ToList()
GridView1.DataSource = lstFiles;
GridView1.DataBind();
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
Sorry, A Typo in my last post :-), Here is the correct one:-

List<string> lstFiles = Directory.GetFiles("Path",string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text),SearchOption.AllDirectories).ToList()
GridView1.DataSource = lstFiles;
GridView1.DataBind();

Open in new window


Here is another option to get you filename and path in an anonymous type:-

var lstFiles =Directory.GetFiles("Path",string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text), SearchOption.AllDirectories).Select(f=>new {FileName = f.Substring(f.LastIndexOf('\\') + 1), FilePath = f}).ToList();

GridView1.DataSource = lstFiles;
GridView1.DataBind();

Open in new window

0
 

Author Comment

by:searchsanjaysharma
Comment Utility
This picks the whole path i want only the file name

List<string> lstFiles = Directory.GetFiles(MapPath("Files/"), string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text), SearchOption.AllDirectories).ToList();
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
Comment Utility
var lstFiles =Directory.GetFiles("Path",string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text), SearchOption.AllDirectories).Select(f=>new {FileName = f.Substring(f.LastIndexOf('\\') + 1), FilePath = f}).ToList();

GridView1.DataSource = lstFiles;
GridView1.DataBind();

Open in new window

0
 

Author Closing Comment

by:searchsanjaysharma
Comment Utility
ok
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now