Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Unable to cast object of type DirectoryInfo to type String

Here we go.

So, I have this code:

        private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {
            if (checkedListBox1.GetItemChecked(i))
            {
                string name = (string)checkedListBox1.Items[i];
                MessageBox.Show(name);
            }
        }
    }

and it's going to read the status of checkedListBox1 items and then return some stuff. So if someone checked two boxes "Schmo, Joe" and "Doe, John", then when I click Button_1, it should pop up a message box reading "Schmo, Joe" and another that reads "Doe, John", right?

Visual Studio doesn't say anything about an error while I'm looking at this code, but when I go to run it, I'm told that it "cast object of type 'System.IO.DirectoryInfo' to type 'System.String'." Which sorta makes sense, but I guess I'm wonder

Oh by the way, checkedListBox1 is populated dynamically based on directories on my HDD, so if I had two folders called "Schmoe, Joe" and "Doe, John", then those would be the clickable checkboxes in the CheckedListBox.

I've tried sending the elements into an array or a list, but I still get the same issue: it does not wanna put DirectoryInfo data into a string to be added to an array. I tried using

checkedListBox1.Text = someString
checkedListBox1.Text = someArray
checkedListBox1.Text = someList
someList.Add(name)

And I couldn't figure out how to add elements to an array, otherwise I would've tried that too.

But none of that worked. Says I can't convert DirectoryInfo into a string.

So yeah. How do I convert DirectoryInfo stuff into a string or an array?

Comments