Delete Selected File from Listbox and Folder

点点圈 提交于 2019-12-08 05:33:42

问题


I want to Delete the Selected File from Listbox and Folder. For now it's only removing it from the Listbox. Now I want it to be removed also from the Folder. Thanks

    private void tDeletebtn_Click(object sender, EventArgs e)

    {
        if (listBox1.SelectedIndex != -1)
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
    }

   private void TeacherForm_Load(object sender, EventArgs e)
    {
        DirectoryInfo dinfo = new DirectoryInfo(@"data\\Teachers\\");
        FileInfo[] Files = dinfo.GetFiles("*.xml");
        foreach (FileInfo file in Files)
        {
            listBox1.Items.Add(file.Name);
        }
    }

回答1:


If your listBox1.Items contains your filepath, you could simply pass it by de-referencing the filepath and delete it using File.Delete like this:

private void tDeletebtn_Click(object sender, EventArgs e)
{
    if (listBox1.SelectedIndex != -1){
        string filepath = listBox1.Items[listBox1.SelectedIndex].ToString();
        if(File.Exists(filepath))
            File.Delete(filepath);            
        listBox1.Items.RemoveAt(listBox1.SelectedIndex);
    }
}

That is, if you add your paths to the listBox1 using FullName instead of using Name:

    DirectoryInfo dinfo = new DirectoryInfo(@"data\\Teachers\\");
    FileInfo[] Files = dinfo.GetFiles("*.xml");
    foreach (FileInfo file in Files)
    {
        listBox1.Items.Add(file.FullName); //note FullName, not Name
    }

If you don't want to not add the full name in the listBox1, you could also store the Folder name separately, since it will not be changed anyway:

string folderName; //empty initialization
.
.
    DirectoryInfo dinfo = new DirectoryInfo(@"data\\Teachers\\");
    FileInfo[] Files = dinfo.GetFiles("*.xml");
    folderName = dinfo.FullName; //here you initialize your folder name
    //Thanks to FᴀʀʜᴀɴAɴᴀᴍ
    foreach (FileInfo file in Files)
    {
        listBox1.Items.Add(file.Name); //just add your filename here
    }

And then you just use it like this:

private void tDeletebtn_Click(object sender, EventArgs e)
{
    if (listBox1.SelectedIndex != -1){
        //Put your folder name here..
        string filepath = Path.Combine(folderName, listBox1.Items[listBox1.SelectedIndex].ToString());
        if(File.Exists(filepath))
            File.Delete(filepath);            
        listBox1.Items.RemoveAt(listBox1.SelectedIndex);
    }
}



回答2:


If you have the proper permissions to access the file, this should work fine enough:

System.IO.File.Delete(listBox1.SelectedItem.ToString());

The above code is applicable only if the ListBoxItem is a string. Otherwise you can consider casting it to your Data class and using the appropriate property. Seeing the code you posted, it is not required.

So your final code will be like this:

private void tDeletebtn_Click(object sender, EventArgs e)

{
    if (listBox1.SelectedIndex != -1)
    {
        System.IO.File.Delete(listBox1.Items[listBox1.SelectedIndex].ToString());
        listBox1.Items.RemoveAt(listBox1.SelectedIndex);
    }
}

See:

  • File.Delete() at MSDN
  • Dot Net Perls on File.Delete()

Make sure, you actually have something selected in your ListBox!



来源:https://stackoverflow.com/questions/35813096/delete-selected-file-from-listbox-and-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!