Solved

How to pick only the filename from FileInfo

Posted on 2012-04-11
7
356 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

23 Experts available now in Live!

Get 1:1 Help Now