任何人都可以解释为什么我的代码不加选择地删除? [英] Can anyone explain why my code deletes indiscriminately?

查看:79
本文介绍了任何人都可以解释为什么我的代码不加选择地删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我对linq很新,我决定制作一个程序,它接收一个文件目录,读入文件扩展名和目标文件的年龄。主要目标是简单地使用文件扩展名删除文件,但只针对较旧的文件(1周龄,3个月或更长时间)。



我已经测试了winform在旧文件中包含linq语句但是它似乎无法区分旧文件。(当然我为这些文件创建了一个备份以防万一有些可怕的错误)。



任何帮助将非常感谢并对于下面代码的呈现感到抱歉,因为它在复制和粘贴后采用了不同的形式。



我尝试过:



Hi there, I'm pretty new to linq and I've decided to make a program which takes in a file directory, read in a file extension and the age of the target file(s). The main objective is to simply delete files using file extension but to only target older files(1 week old, 3 month old or more).

I've tested the winform which houses the linq statement on older files however it seems to be unable to distinguish the old from the new.(of course I created a back up for those files in case some goes horrible wrong).

Any help would be greatly appreciated and sorry for the presentation of the code below as it has taken on a different form after the copy and paste.

What I have tried:

protected void log()
        {
           
            string path = fileLocation.Text;
            string fullPath = string.Format(@"{0}\File_Deletion_Log.txt", path);
            File.AppendAllLines(fullPath, new[] {"File Deletion Complete    " + string.Format("{0:yyyy-MM-dd}   ",DateTime.Now) + string.Format("Files Deleted: {0}",Count)});
            Count = 0;
        }
        protected void button1_Click(object sender, EventArgs e)
        {   
                string extensions = fileExtension.Text;

                    var files = new DirectoryInfo(fileLocation.Text).GetFiles("*", SearchOption.AllDirectories)
                                     .Select(p => new FileInfo(Convert.ToString(p)))
                                     .Where(p => p.CreationTime < DateTime.Now.Subtract(TimeSpan.FromDays(Convert.ToInt32(fileAge.Text))))
                                     .Where(p => string.Compare(p.Extension, extensions, true) == 0);
                    foreach (var file in files)
                    {
                        file.Attributes = FileAttributes.Normal;
                        File.Delete(file.FullName);
                        Count++;
                    }
                    MessageBox.Show(string.Format("Total Number of files deleted:   {0}", Count));
                    log();

推荐答案

如果没有您的文件进行测试,我们根本无法做太多 - 所以开始通过将Linq分解为单独的语句:

Without your files to test against, we can't do much at all - so start by "breaking down" the Linq into separate statements:
var files = new DirectoryInfo(fileLocation.Text).GetFiles("*", SearchOption.AllDirectories);
var selected = files.Select(p => new FileInfo(Convert.ToString(p))).ToList();
DateTime limit = DateTime.Now.Subtract(TimeSpan.FromDays(Convert.ToInt32(fileAge.Text)));
var where = selected.Where(p => p.CreationTime < limit).ToList();
var where2 = where.Where(p => string.Compare(p.Extension, extensions, true) == 0).ToList();

然后,您可以逐步查看流程中的每个阶段,以确切了解正在处理的内容以及您的结果得到。



希望,这会给你一些关于问题所在的信息。

Then you can step through looking at each stage in the process to see exactly what is being processed and what results you get.

Hopefully, that will give you some info on where teh problem is.


目前尚不清楚你的 fileAge 来自但您可以尝试 TimeSpan.FromDays(90)来缩小问题范围。并在调试时跳过删除。只需打印文件名。
It is not clear where your fileAge comes from but you could try TimeSpan.FromDays(90) to narrow the problem down. And skip the delete while debugging. Just print the filenames.


这篇关于任何人都可以解释为什么我的代码不加选择地删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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