Skip to content

How do I remove item from list box and File Directory

Steve Johnson

Asked Apr 10, 2012, 1:44 PM EDT1 answer
Share
LinkedInX
Hello.


I have a program which creates a random text file name, places it in a list box and places the file in my documents.


I have a delete button which deletes the file from the list box but doesn't delete it from my documents. How do I read the file from the list box and ensure it's the same one as in my documents, then delete  it if the button is clicked.


Thanks


P.S i'm new to these forums, I don't know how to turn code text type off lol

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    0
    Hi,

    For delete the items in the ListBox you can use the following statements.

    TypeScript
        protected void btndeleteItem_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Remove(new ListItem(ListBox1.SelectedItem.Text));
        }

    For check the file exits in the directory or not,

            if (System.IO.File.Exists(ListBox1.SelectedItem.Text))
            {
                File.Delete(ListBox1.SelectedItem.Text);
            }

    Here if you have full path of the file in the listbox then you can use directly as path. Otherwise you know the path where it is stored then you can hard code the path along with the selected file in the list box.

    Hope this will help you.

    0