How to display the files in a specific folder?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to display the files in a specific folder?

  1. #1
    Join Date
    Jan 2011
    Posts
    15

    Question How to display the files in a specific folder?

    I'm trying to make a little disk cleaner program and I couldn't figure out how to display the results of scanned directory on list view tool on windows form application. I have a 3 columns and these are File Name, Folder Path and File Size. I want the program to scan for example this location: C:\Users\Emre\AppData\Local\CrashDumps and display all of the files in that location on the list view tool. How could i do this? And how do display the file size and file location on the next 2 colomns and these other two colomns are sub items.

    Thank you!

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: How to display the files in a specific folder?

    An array of objects containing all the information you want (and more) about the files in a given directory can be obtained by using the DirectoryInfo::GetFiles() method. The DirectoryInfo instance to call the method on can be created by passing the path of the directory of interest to the DirectoryInfo constructor.

    To display columns two and three of your list: Yes, you'd make them subitems. Just make sure the list view is set to report mode and actually has columns for the subitems, or they won't be displayed.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jan 2011
    Posts
    15

    Talking Re: How to display the files in a specific folder?

    Thank you for your answer, I have a rough idea about how I could do it but is it possible for you to give me the code for it, because now I would have search the code really

    Thanks

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: How to display the files in a specific folder?

    The following code would fill an existing list view named listView1 (like the IDE would name it by default) with items containing subitems containing the information you want according to post #1. It's not tested, though.

    Code:
    String ^strDirectoryPath;
    
    // Here the path to the directory of interest gets assigned to strDirectoryPath somehow...
    
    DirectoryInfo ^di = gcnew DirectoryInfo(strDirectoryPath);
    for each (FileInfo ^fi in di->GetFiles())
      listView1->Items->Add(gcnew ListViewItem(gcnew array<String ^>{fi->Name, fi->DirectoryName, fi->Length.ToString()});
    Naturally, I can't write the rest ofthe code for you (or even design your GUI), not only because I don'z have enough information to do so, but mainly because, of course, you should write the majority of your program yourself.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center