Solved

How to pick only the filename from FileInfo

Posted on 2012-04-11
7
367 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 17

Expert Comment

by:Anuroopsundd
ID: 37831347
0
 
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
Technology Partners: 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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