我无法从系统中删除临时文件,但我可以从应用程序中删除 [英] I Can't Delete Temporary Files From The System But I Can Delete From Application

查看:99
本文介绍了我无法从系统中删除临时文件,但我可以从应用程序中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Windows应用程序,我想要删除临时文件,我可以从listview中删除文件,但我无法从系统中删除文件,它再次显示系统中的相同文件

这是我删除的代码

i created one windows application in that i want to delete the temporary files i can delete the files from listview but i can't delete files from the system it showing again same files in the system
this is my code under delete

private void button1_Click(object sender, EventArgs e)
{
    var tmpPath = Environment.GetFolderPath(Environment.SpecialFolder.Recent);

    string[] files = Directory.GetFiles(tmpPath, "*.*", SearchOption.AllDirectories);

    listView1.Items.AddRange((files.Select(f => new ListViewItem(f))).ToArray());
}

private void btndelete_Click(object sender, EventArgs e)
{
    if (listView1.SelectedItems.Count > 0)
    {
        var confirmation = MessageBox.Show("Do You Really Want to Delete the Item", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (confirmation == DialogResult.Yes)
        {
            for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
            {
                ListViewItem itm = listView1.SelectedItems[i];
                listView1.Items[itm.Index].Remove();
            }
        }
        else
            MessageBox.Show("None Selected");
    }
}

推荐答案

在你的代码中,你只是从列表视图中删除项目,但你是不删除任何东西。你必须使用File.Delete,如下所示:



In your code you are just removing the items from the listview, but you are not deleting anything. You must use File.Delete, something like this:

if (listView1.SelectedItems.Count > 0)
{
var confirmation = MessageBox.Show("Do You Really Want to Delete the Item", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirmation == DialogResult.Yes)
{
for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem itm = listView1.SelectedItems[i];

File.Delete(itm.Text.ToString()); 

listView1.Items.Remove(itm);
}
 
}
else
MessageBox.Show("None Selected");
}


这篇关于我无法从系统中删除临时文件,但我可以从应用程序中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆