Results 1 to 14 of 14

Thread: open directory

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    open directory

    hey guys im trying to open a directory to list the images in it ..i have this up to know

    Code:
    Dim strFileSize As String = ""
            Dim di As New IO.DirectoryInfo("C:\Users\TECHKER\Pictures")
            Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
            Dim fi As IO.FileInfo
    
            For Each fi In aryFi
                If fi.Extension = ".jpg" Or fi.Extension = ".gif" Or fi.Extension = ".bmp" Then ''etc etc etc
                    strFileSize = (Math.Round(fi.Length / 1024)).ToString()
                    Console.WriteLine("File Name: {0}", fi.Name)
                    Console.WriteLine("File Full Name: {0}", fi.FullName)
                    Console.WriteLine("File Size (KB): {0}", strFileSize)
                    Console.WriteLine("File Extension: {0}", fi.Extension)
                    Console.WriteLine("Last Accessed: {0}", fi.LastAccessTime)
                    Console.WriteLine("Read Only: {0}", (IO.FileAttributes.ReadOnly = True).ToString)
                End If
            Next

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: open directory

    I used a listbox rather than writing it to the console, but other than that, I used the code you have and didn't experience any problems.

    You told us what you were trying to do, and what you were trying to do it with. What you haven't told us is what happens when you make the try. What isn't working?

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: open directory

    list box is a good idea but would it show images in a given directory?that is what i need..this code does nothing..

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: open directory

    If you need to display the associated icons then:
    Easy way:
    vb.net Code:
    1. dim i As Icon  = Icon.ExtractAssociatedIcon("filename")

    Hard (but True) way:
    Google for 'system imagelist'

    If you want to display the images then you won't be able to do that in console window.
    You'd need to create a form and display it there.

    An image is loaded from file using this code:
    vb Code:
    1. Dim img As Image = Image.FromFile("filename.jpg")

    The obtained image is displayed in the paint event handler like this:

    vb.net Code:
    1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2.     e.Graphics.DrawImage(img)
    3. End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: open directory

    i will google system imagelist and keep you posted.thx for guiding me .

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: open directory

    ok so i got some thing here.

    Code:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim Allfiles As String() = IO.Directory.GetFiles("C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\trainersTool\trainersTool\Images")
            Dim strfile As String
            For Each strfile In Allfiles
                Dim filename As New IO.FileInfo(strfile) '/// this will give you the file name etc...
                ListBox1.Items.Add(filename.Name)
            Next
        End Sub
    but it doesn't show anything..

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: open directory

    It WON'T draw images for you. You should tell with your code how and where to draw them.
    Your code simply adds file names into a Listbox, that's all. You have to code where and how do you want your images to be drawn.
    As I already said, if you want ICONS then use ExtractAssociatedIcon method to extract icons and then turn on the OwnerDraw property and draw each listbox element yourself.
    If you want to draw actual images (jpgs, etc) you will have to LOAD these images from disk into an IMAGE object and then DRAW them somewhere.

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: open directory

    You realise you have got this code in the SelectedIndexChanged event handler right? So that code will only be executed when you select an item in the listbox, which if you have no items in it in the first place, is impossible to do.

    cicatrix - I dont think he wants to draw them, just display the file names in a listbox.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: open directory

    lost..sorry

    but even the file name does not show..
    so need to google:
    ExtractAssociatedIcon method
    OwnerDraw property

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: open directory

    Quote Originally Posted by chris128 View Post
    You realise you have got this code in the SelectedIndexChanged event handler right? So that code will only be executed when you select an item in the listbox, which if you have no items in it in the first place, is impossible to do.

    cicatrix - I dont think he wants to draw them, just display the file names in a listbox.
    i would like both

    so were do i put the code?

  11. #11
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: open directory

    Hang it on a button_click.

    Then change the OwnerDraw property to your listbox
    Then change the DrawMode of your listbox to OwnerDrawVariable (or OwnerDrawFixed), depending on the size of an item you want to display be fixed or variable.

    Then handle the DrawItem event of the Listbox.

    Now, the most important part - read at least something about GDI+ or you'll be asking questions forever.

    Use e.Graphics object to get access to drawing methods.
    Look my earlier posts for information about how to load an image from file (or extract associated icon) and how to draw it using the graphics object.

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: open directory

    is there any examples of this?im very visual..

  13. #13

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: open directory

    haha!thats cool.

    thx

Posting Permissions

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



Click Here to Expand Forum to Full Width