?
Solved

How to pick only the filename from FileInfo

Posted on 2012-04-11
7
Medium Priority
?
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37831364
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
ID: 37831396
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37831429
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
ID: 37831447
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 2000 total points
ID: 37831473
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
ID: 37831524
ok
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 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