Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to pick only the filename from FileInfo

Posted on 2012-04-11
7
Medium Priority
?
387 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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…

609 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