【问题标题】:Delete Selected File from Listbox and Folder从列表框和文件夹中删除选定的文件
【发布时间】:2016-03-05 10:43:09
【问题描述】:

我想从列表框和文件夹中删除选定的文件。现在它只是从列表框中删除它。现在我希望它也从文件夹中删除。谢谢

    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);
        }
    }

【问题讨论】:

标签: c# xml listbox delete-file selectedindex


【解决方案1】:

如果您的listBox1.Items 包含您的文件路径,您可以简单地通过取消引用filepath 来传递它,然后使用File.Delete 将其删除,如下所示:

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);
    }
}

也就是说,如果您使用FullName 而不是使用Name 将路径添加到listBox1

    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
    }

如果您不想在listBox1 中添加全名,您也可以单独存储Folder 名称,因为无论如何它都不会更改:

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
    }

然后你就这样使用它:

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);
    }
}

【讨论】:

  • 就像它仍然只从我的列表框中删除它们
  • @Luke_i 你能展示一下listBox1 中的文字是什么样子的吗?这里的关键字是fullpath。如果它是错误的路径,那么这是由于路径不存在,那么您根本无法删除该文件...
  • 它将它们列为“Test.xml”、“Test2.xml”,它们都在彼此的下方
  • 但是如果它正在加载它们,那么我不能有错误的文件路径......我猜
  • @Luke_i 就是这样!那里没有文件夹路径!你看到了吗? ;) 这是因为当你第一次在文件中时,你只添加了file.Name。如果你像这样添加它:listBox1.Items.Add(file.FullName); 那么你就有了完整路径
【解决方案2】:

如果您具有访问文件的适当权限,这应该可以正常工作:

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

仅当ListBoxItem 为字符串时,上述代码才适用。否则,您可以考虑将其转换为您的 Data 类并使用适当的属性。看你贴的代码,不需要。

所以你的最终代码会是这样的:

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);
    }
}

见:

确保您在ListBox 中确实选择了某些内容!

【讨论】:

  • 就像它给了我一个空引用异常未处理 - 对象引用未设置为对象的实例。
  • 好的,文件夹路径的功劳归您所有.. ;) 投了赞成票。
  • 不,不,我的意思是,重要的是顺序,而不是使用SelectedItem还是SelectedIndex
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多