Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:16:17+00:00 2026-06-09T18:16:17+00:00

This code is meant to read in a folder directory from a text file

  • 0

This code is meant to read in a folder directory from a text file I have stored, and using that ‘activeFilepath’ variable to read through each file in the folder and retrieve their names, which in turn can be used to load each image up in turn.

StreamReader textIn = new StreamReader(imageSet);
try
{
    //Get the folder location by reading in the specific text file.
    activeFilepath = textIn.ReadLine();
    //If the user isn't choosing a password then go into the 'Blurred' directory of the folder.
    if (choosePassword == false)
       activeFilepath = activeFilepath + @"\Blurred";

    int i = 0;
    //Read in all of the image location strings, and put them in the pictureLocations list.
    foreach (string filePath in Directory.EnumerateFiles(activeFilepath))
    {
        pictureLocations.Add(filePath);
        //pictureLocations[i] = filePath;

        // Display an error image if there is a blank entry in the picture location.
        if (pictureLocations[i] == null)
            pictureLocations[i] = @"C:\Users\Public\Pictures\Sample Pictures\error.png";
        //Remove Thumbs.db from the list if read in.
        if (pictureLocations[i] == activeFilepath + @"\Thumbs.db")
        {
            pictureLocations.Remove(pictureLocations[i]);
        }

        i++;
    }

    foreach (PictureBox imagePanel in pictureBoxList)
    {
        int rand = randomGenerator.Next(0, pictureLocations.Count);
        //The images are then loaded in a random order from the picture location list.
        imagePanel.Load(pictureLocations[rand]);

        //Remove the picture just loaded out of the list so it won't repeat.
        pictureLocations.Remove(pictureLocations[rand]); 
        imagePanel.SizeMode = PictureBoxSizeMode.StretchImage;
    }

At the moment I’m receiving an index type error saying the Index is out of range when I remove Thumbs.db from my list (because I don’t want to load it into the picture boxes I have on my form), however when I simply let it read in it in, I receive a parameter-type error instead.

I’m assuming I get the latter because it’s trying to load Thumbs.db as an image, but I’m wondering if there’s another way of removing or stopping it from being read in the first place?
Is it something else entirely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:16:18+00:00Added an answer on June 9, 2026 at 6:16 pm

    The crash is that you increment i even when you didn’t add anything. A simple fix would be simply not to add the item if you don’t intend to keep it. That way you don’t need the pesky i variable.

    //Read in all of the image location strings, and put them in the pictureLocations list.
    foreach (string filePath in Directory.EnumerateFiles(activeFilepath))
    {
        // Display an error image if there is a blank entry in the picture location.
        if (filePath == null)
            filePath = @"C:\Users\Public\Pictures\Sample Pictures\error.png";
        //Skip Thumbs.db
        if (filePath == activeFilepath + @"\Thumbs.db")
        {
            continue;
        }
    
        pictureLocations.Add(filePath);
    }
    

    In other words, rather than adding an item and then later realizing you didn’t mean to add it (and having to keep track of where it is), simply decide up front whether you want to add it or not (and if it needs to be edited), and then add it if you want to keep it.

    This avoids the fragile relationship between pictureLocation and i.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got this read file code from microsoft @C:\Users\computing\Documents\mikec\assignment2\task_2.txt That works fine when
I have this code that opens a directory and checks if the list is
I have a code meant to strim a string within a large text file.
DISCLOSURE: This is homework. The code below is meant to read a command file
I have no idea what this means. But here is the code that it
This code runs fine but the for statement by the meant to read back
First, I have researched this question a lot. I learned how to read from
Im using the 960 css framework. I have this code: <div class=container_16 id=header> div
Question from the one interview. Please explain what does this C++ code mean: void
I mean I have this code: <script> $(document).ready( function() { $('html, body').animate({ scrollTop: $(.error).offset().top

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.