getfiles, minus the path?

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2hyaXM=?=, Dec 29, 2004.

  1. I'mtrying to display a list of file names in a directory but using getfiles
    displays the entire path. Is there any way to just display the file name?
    I'm putting the file names into a datagrid:

    'create file list for defect images datagrid
    Dim imagepath As String
    imagepath = Server.MapPath("\xxxxx\xxxxxx\examples\")
    dgfiles.DataSource = Directory.GetFiles(imagepath)
    dgfiles.DataBind()

    I I'm thinking is with the datagrid I can change the displayed column value
    and make it a link to display an image based on the file click on in the
    datagrid.

    Also I wish the dropdownlist allowed a separate value parameter to set, i.e.
    display = "file.gif", value for item = "c:/xxx/xxxx/file.gif".

    thanx.
     
    =?Utf-8?B?Q2hyaXM=?=, Dec 29, 2004
    #1
    1. Advertising

  2. Hi Chris,

    I think you just need two more bits to have this figured out. First, you can
    make changes in the content of the datagrid row when the row is bound to its
    data. Therefore, using the ItemDataBound event.

    Next, you want to strip off all the path stuff. That's done easily with
    System.IO.Path.GetFileName.

    When you put them together, you get something like the code below.

    Let us know if this helps?

    Ken
    Microsoft MVP ASP.NET
    Toronto


    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    'create file list for defect images datagrid
    Dim imagepath As String
    imagepath = Server.MapPath("bin")
    dgfiles.DataSource = _
    System.IO.Directory.GetFiles(imagepath)
    dgfiles.DataBind()

    End Sub

    Private Sub dgfiles_ItemDataBound _
    (ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles dgfiles.ItemDataBound
    Dim strFilename As String
    strFilename = System.IO.Path.GetFileName(e.Item.Cells(0).Text)
    e.Item.Cells(0).Text = strFilename
    End Sub

    <asp:datagrid id="dgfiles" runat="server"></asp:datagrid>


    "Chris" <> wrote in message
    news:...
    > I'mtrying to display a list of file names in a directory but using
    > getfiles
    > displays the entire path. Is there any way to just display the file name?
    > I'm putting the file names into a datagrid:
    >
    > 'create file list for defect images datagrid
    > Dim imagepath As String
    > imagepath = Server.MapPath("\xxxxx\xxxxxx\examples\")
    > dgfiles.DataSource = Directory.GetFiles(imagepath)
    > dgfiles.DataBind()
    >
    > I I'm thinking is with the datagrid I can change the displayed column
    > value
    > and make it a link to display an image based on the file click on in the
    > datagrid.
    >
    > Also I wish the dropdownlist allowed a separate value parameter to set,
    > i.e.
    > display = "file.gif", value for item = "c:/xxx/xxxx/file.gif".
    >
    > thanx.
     
    Ken Cox [Microsoft MVP], Dec 30, 2004
    #2
    1. Advertising

  3. Thanx, this works great. It sure beats the routine I wrote to strip off the
    file name, etc.
    Also, do you know how to access the column info. The datagrid's header is
    "Item". Normally I would use <asp:BoundColumn... but that's with a datasource
    coming from a database. How do you handle it from the file object?
    I'd like to change the header and make the column a link to the actual file.
    I've done it via a database datasource.

    Thanx.

    "Ken Cox [Microsoft MVP]" wrote:

    > Hi Chris,
    >
    > I think you just need two more bits to have this figured out. First, you can
    > make changes in the content of the datagrid row when the row is bound to its
    > data. Therefore, using the ItemDataBound event.
    >
    > Next, you want to strip off all the path stuff. That's done easily with
    > System.IO.Path.GetFileName.
    >
    > When you put them together, you get something like the code below.
    >
    > Let us know if this helps?
    >
    > Ken
    > Microsoft MVP ASP.NET
    > Toronto
    >
    >
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) Handles MyBase.Load
    > 'create file list for defect images datagrid
    > Dim imagepath As String
    > imagepath = Server.MapPath("bin")
    > dgfiles.DataSource = _
    > System.IO.Directory.GetFiles(imagepath)
    > dgfiles.DataBind()
    >
    > End Sub
    >
    > Private Sub dgfiles_ItemDataBound _
    > (ByVal sender As Object, _
    > ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    > Handles dgfiles.ItemDataBound
    > Dim strFilename As String
    > strFilename = System.IO.Path.GetFileName(e.Item.Cells(0).Text)
    > e.Item.Cells(0).Text = strFilename
    > End Sub
    >
    > <asp:datagrid id="dgfiles" runat="server"></asp:datagrid>
    >
    >
    > "Chris" <> wrote in message
    > news:...
    > > I'mtrying to display a list of file names in a directory but using
    > > getfiles
    > > displays the entire path. Is there any way to just display the file name?
    > > I'm putting the file names into a datagrid:
    > >
    > > 'create file list for defect images datagrid
    > > Dim imagepath As String
    > > imagepath = Server.MapPath("\xxxxx\xxxxxx\examples\")
    > > dgfiles.DataSource = Directory.GetFiles(imagepath)
    > > dgfiles.DataBind()
    > >
    > > I I'm thinking is with the datagrid I can change the displayed column
    > > value
    > > and make it a link to display an image based on the file click on in the
    > > datagrid.
    > >
    > > Also I wish the dropdownlist allowed a separate value parameter to set,
    > > i.e.
    > > display = "file.gif", value for item = "c:/xxx/xxxx/file.gif".
    > >
    > > thanx.

    >
    >
     
    =?Utf-8?B?Q2hyaXM=?=, Dec 30, 2004
    #3
  4. Hi Chris,

    To change the header, check for the Header type and then insert your new
    text. Here's the updated code:

    Private Sub dgfiles_ItemDataBound _
    (ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles dgfiles.ItemDataBound
    If e.Item.ItemType = ListItemType.Header Then
    e.Item.Cells(0).Text = "New Header Here"
    Exit Sub
    End If
    If e.Item.ItemType = ListItemType.AlternatingItem Or _
    e.Item.ItemType = ListItemType.Item Then
    Dim strFilename As String
    strFilename = System.IO.Path.GetFileName(e.Item.Cells(0).Text)
    e.Item.Cells(0).Text = strFilename
    End If
    End Sub

    Ken
    MVP [ASP.NET]
    Toronto


    "Chris" <> wrote in message
    news:...
    > Thanx, this works great. It sure beats the routine I wrote to strip off
    > the
    > file name, etc.
    > Also, do you know how to access the column info. The datagrid's header is
    > "Item". Normally I would use <asp:BoundColumn... but that's with a
    > datasource
    > coming from a database. How do you handle it from the file object?
    > I'd like to change the header and make the column a link to the actual
    > file.
    > I've done it via a database datasource.
    >
    > Thanx.
    >
     
    Ken Cox [Microsoft MVP], Dec 30, 2004
    #4
  5. thanx for the code start! It really helped me understand.

    "Ken Cox [Microsoft MVP]" wrote:

    > Hi Chris,
    >
    > To change the header, check for the Header type and then insert your new
    > text. Here's the updated code:
    >
    > Private Sub dgfiles_ItemDataBound _
    > (ByVal sender As Object, _
    > ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    > Handles dgfiles.ItemDataBound
    > If e.Item.ItemType = ListItemType.Header Then
    > e.Item.Cells(0).Text = "New Header Here"
    > Exit Sub
    > End If
    > If e.Item.ItemType = ListItemType.AlternatingItem Or _
    > e.Item.ItemType = ListItemType.Item Then
    > Dim strFilename As String
    > strFilename = System.IO.Path.GetFileName(e.Item.Cells(0).Text)
    > e.Item.Cells(0).Text = strFilename
    > End If
    > End Sub
    >
    > Ken
    > MVP [ASP.NET]
    > Toronto
    >
    >
    > "Chris" <> wrote in message
    > news:...
    > > Thanx, this works great. It sure beats the routine I wrote to strip off
    > > the
    > > file name, etc.
    > > Also, do you know how to access the column info. The datagrid's header is
    > > "Item". Normally I would use <asp:BoundColumn... but that's with a
    > > datasource
    > > coming from a database. How do you handle it from the file object?
    > > I'd like to change the header and make the column a link to the actual
    > > file.
    > > I've done it via a database datasource.
    > >
    > > Thanx.
    > >

    >
    >
     
    =?Utf-8?B?Q2hyaXM=?=, Dec 30, 2004
    #5
  6. Hey Chris,

    Drop in any time. We've got a great group here. We all need a boost at some
    point.

    Ken

    "Chris" <> wrote in message
    news:...
    > thanx for the code start! It really helped me understand.
    >
    > "Ken Cox [Microsoft MVP]" wrote:
     
    Ken Cox [Microsoft MVP], Dec 30, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: GetFiles()

    Kevin Spencer, Aug 5, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,909
    Yan-Hong Huang[MSFT]
    Aug 11, 2003
  2. Alphonse Giambrone

    Multiple Search Pattern for Directory.GetFiles

    Alphonse Giambrone, Jan 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    36,854
    MrZedSven
    May 5, 2011
  3. =?Utf-8?B?aHV6eg==?=

    Problem with GetFiles

    =?Utf-8?B?aHV6eg==?=, Dec 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    539
    Kevin Spencer
    Dec 2, 2004
  4. Ken Cox [Microsoft MVP]

    Re: DirectoryInfo.GetFiles(Mask) finding too many files!

    Ken Cox [Microsoft MVP], Jan 26, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    828
    Ken Cox [Microsoft MVP]
    Jan 26, 2005
  5. =?Utf-8?B?RGFuaWVsIENvcnLDqmE=?=

    Directory.GetFiles() / DirectoryInfo.GetFiles() on a remote server

    =?Utf-8?B?RGFuaWVsIENvcnLDqmE=?=, Mar 22, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    1,980
    BradM
    Mar 24, 2007
Loading...

Share This Page