1

so I got a bunch of files here in one directory.

They are all named like this:

  • Daniel-2013-09-10.jpg
  • Peter-2012-05-06.jpg
  • Christiane-2011-01-08.jpg

So I got all of these items in one directory and put them into one array:

string[] pictures = Directory.GetFiles(@"C:/Pictures", "*.jpg");

I got a ListView with 3 columns, Name, Date and File size.

I want to get all these information from the file names and then put them into the listview. So for those three files it would look like this:

Name--------------Date---------------------------File size
Daniel-------------10. September 2013--------26 KB
Peter--------------06. May 2012-----------------39 KB
Christiane--------08. January 2011------------35 KB

So I thought of splitting the information in the array with foreach and then using another loop to write the data in the the ListView, but I don't know exactly how to do that.

Any help is appreciated ^^

Cheers

1
  • 1
    You're kinda asking us to do it for you ... we're here to help with specific problems - a place you've got stuck. Commented Oct 9, 2013 at 9:48

3 Answers 3

5

I'm feeling overly generous.. normally I would say give it a start yourself.. but this seemed like a fun thing to throw together before bed.

class PictureLoader {
    private readonly string[] _images;

    public PictureLoader(string path) {
        _images = Directory.GetFiles(path, "*.jpg");
    }

    public IEnumerable<Tuple<string, string, string>> GetRowData() {
        foreach (var imagePath in _images) {
            var extension = Path.GetExtension(imagePath);
            var fileName = Path.GetFileName(imagePath);
            var regex = Regex.Match(fileName, @"([A-Za-z]+)-(\d{4}-\d{2}-\d{2})(.[A-Za-z]+)");
            var name = regex.Groups[1].Value;
            var date = DateTime.ParseExact(regex.Groups[2].Value, "yyyy-MM-dd", CultureInfo.InvariantCulture);

            yield return
                new Tuple<string, string, string>(name + extension, date.ToString(),
                    (new FileInfo(imagePath).Length / 1024).ToString() + " KB");
        }
    }
}

Usage:

var pictureLoader = new PictureLoader(@"folder here");

foreach (var group in pictureLoader.GetRowData()) {
    var item = new ListViewItem();
    item.Text = group.Item1;
    item.SubItems.Add(group.Item2);
    item.SubItems.Add(group.Item3);

    listView1.Items.Add(item);
}

Results in:

end result

This is a starting point for you. I will leave the tiny details I missed to you.

Sign up to request clarification or add additional context in comments.

2 Comments

I realise this is probably frowned upon (hence the downvote). I will remove it :(
I undeleted because the downvote turned into an upvote.. can someone advise if this sort of answer is not welcome? This is the first of its kind from me..
1

foreach is good. Then you need to parse the string into your variables. Then you need to add to the listview. Try googling "listview c# add item"

Comments

0

I think you can go over all files with a foreach. In each Loop you have to split the Name of the file by using a regex for Splitting the name.

The File Size you can get via code.

And so you are able to show the relevant data in the grid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.