List files in a directory, modify name.

Discussion in 'ASP .Net' started by Øyvind Isaksen, Feb 10, 2006.

  1. Hi,

    I have made a code that list all files in a directory on the server.
    It works fine, but I need to modify the filename (remove the 7 first char in
    the name) that is displayed in the datagrid.

    Example:
    The file "123456_myfile.jpg" shoult be displayed like this: "myfile.jpg"

    ---------------------------
    This is my code so far:
    ---------------------------

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim dirInfo As New
    DirectoryInfo(Server.MapPath("/uploads/isaksenmedia/"))
    Me.dtgFilelist.DataSource = dirInfo.GetFiles("*.*")
    Me.dtgFilelist.DataBind()
    End Sub


    <asp:DataGrid id="dtgFilelist" runat="server" autogeneratecolumns="False">
    <Columns>
    <asp:HyperLinkColumn DataNavigateUrlField="Name"
    DataTextField="Name" HeaderText="Filename" />
    <asp:BoundColumn DataField="LastWriteTime" HeaderText="Modyfied"
    DataFormatString="{0:d}" />
    <asp:BoundColumn DataField="Length" HeaderText="Size"
    DataFormatString="{0:#,### bytes}" />
    </Columns>
    </asp:DataGrid>



    Thanks!!!!!!
     
    Øyvind Isaksen, Feb 10, 2006
    #1
    1. Advertising

  2. Øyvind Isaksen

    DKode Guest

    If i were you, I would dump the GetFiles into a collection first and
    iterate through that, changing the names while it is still in a
    collection, then bind this collection to the datagrid, it would be
    easier then binding it to the datagrid and then changing it.
     
    DKode, Feb 10, 2006
    #2
    1. Advertising

  3. Ok, I sounds like a smart way to do it!
    Could you please help me get started making this "collection", I'm not shure
    how to do this... Guess this is an array...?

    Thanks alot!!!



    "DKode" <> wrote in message
    news:...
    > If i were you, I would dump the GetFiles into a collection first and
    > iterate through that, changing the names while it is still in a
    > collection, then bind this collection to the datagrid, it would be
    > easier then binding it to the datagrid and then changing it.
    >
     
    Øyvind Isaksen, Feb 10, 2006
    #3
  4. Øyvind Isaksen

    DKode Guest

    ok,

    GetFiles returns a FileInfo[] collection

    if you go to msdn.microsoft.com and look up FileInfo class, you'll see
    all the different properties you can get about a file, it depends on
    what you want out of FileInfo class. you can setup a foreach and
    iterate through the FileInfo[] collection changing properties if you
    want:

    i havent tested this code, but you should point you in the right
    direction

    FileInfo[] files = dirInfo.GetFiles();

    foreach (FileInfo f in files) {
    f.Name = f.Name.substring(8, f.Name.Length - 8);
    }

    DataGrid myDataGrid.DataSource = files;
    myDataGrid.DataBind();

    that substring might throw an error because of the length of the
    substring, but you get the idea.

    hope this helps
     
    DKode, Feb 10, 2006
    #4
    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. TJS
    Replies:
    1
    Views:
    528
    Eliyahu Goldin
    Jun 23, 2004
  2. Steve Singer
    Replies:
    0
    Views:
    552
    Steve Singer
    Feb 15, 2005
  3. Shaker
    Replies:
    1
    Views:
    115
    Patrick Olurotimi Ige
    Dec 28, 2004
  4. Brian Wallace
    Replies:
    10
    Views:
    265
    Martin DeMello
    Apr 21, 2009
  5. iMath
    Replies:
    8
    Views:
    304
    emile
    Nov 13, 2012
Loading...

Share This Page