Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts

    asp.net dirlist to array

    im trying to get a list of (jpg) files in a directory into an array so that i can select one at random to display

    is there any easy way of doing this? i found an example on 4guysfromrolla but i get an error when i try and display it, all the rest of the examples say they are in an array but just display it with a datagrid so i dont know how to get the fillenames out

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Cincinnati, OH
    Posts
    545
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This will grab files from a directory with a ".jpg" extension and then select a random file.

    Code:
            
            Dim dir() As String = Directory.GetFiles(Server.MapPath("\"), "*.jpg")
            Dim rand As Random = New Random
            If dir.Length > 0 Then
                Response.Write(dir(rand.Next(0, dir.Length)))
            End If
    If you are wanting to grab just the file name look into the Path class (Path.GetFileName). Is this what you were needing?
    does this sig match?

  • #3
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    thisis what ithought it should be but i get an error in the first line of that

    (btw i change it to .\ instead of just \ to get current dir instead of root right.. i know this works in classic asp anyway

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Cincinnati, OH
    Posts
    545
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What does the error say?
    does this sig match?

  • #5
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    its just the standard asp.net error (i think i need to put a .asa somewhere or something to show a detailed error but i dont know what i have to put where )

  • #6
    Regular Coder
    Join Date
    Jul 2004
    Location
    France
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Public JPGPATH As String = HttpRuntime.AppDomainAppPath + "images\jpg\"

    Dim di As New DirectoryInfo(JPGPATH)
    Dim finf As FileInfo() = di.GetFiles()

    For Each fiTemp As FileInfo In finf
    ... do what you like
    Next

  • #7
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    still get standard asp.net error.. how do i get real errors to display, and do i need to import anything at the top or anything?

    [error still in ...Dim di As New DirectoryInfo(JPGPATH).. line]

  • #8
    Regular Coder
    Join Date
    Jul 2004
    Location
    France
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is there any rights problems on the folder ?

  • #9
    Regular Coder
    Join Date
    Jul 2004
    Location
    France
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Private Function DisplayJpg() As String
    Dim Folder As String = "images\random\"
    Dim JPGPATH As String = HttpRuntime.AppDomainAppPath + Folder
    Dim di As New System.io.DirectoryInfo(JPGPATH)
    Dim finf As System.io.FileInfo() = di.GetFiles()
    Dim sc As New System.Collections.specialized.StringCollection
    For Each fiTemp As System.io.FileInfo In finf
    sc.Add(fiTemp.ToString)
    Next
    If sc.Count > 0 Then
    Dim rand As Random = New Random
    Dim num As Int32 = rand.Next(0, sc.Count - 1)
    Return String.Format("<img src=""{0}{1}"" alt="""" />", Folder, sc(num))
    Else
    Return String.Format("<img src=""{0}{1}"" alt="""" />", Folder, "default.jpg")
    End If
    End Function
    Last edited by gwendaal; 12-16-2004 at 07:27 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •