简体   繁体   English

尝试从名称匹配\\的单个目录中删除多个文件包含特定字符串

[英]Trying to delete multiple files from a single directory whose names match\contain a particular string

I wish to delete image files. 我想删除图像文件。 The files are named somefile.jpg and somefile_t.jpg , the file with the _t on the end is the thumbnail. 这些文件名为somefile.jpgsomefile_t.jpg ,最后带_t的文件是缩略图。 With this delete operation I wish to delete both the thumbnail and original image. 使用此删除操作,我希望删除缩略图和原始图像。

The code works up until the foreach loop, where the GetFiles method returns nothing. 代码一直运行到foreach循环, GetFiles方法返回任何内容。 The string.Substring operation successfully returns just the file name with no extension and no _t eg: somefile . string.Substring操作成功返回没有扩展名的文件名,也没有_t例如: somefile

There are no invalid characters in the file names I wish to delete. 我想删除的文件名中没有无效字符。 Code looks good to me, only thing I can think of is that I am somehow not using the searchpattern function properly. 代码对我来说很好,只有我能想到的是我在某种程度上没有正确使用searchpattern函数。

filesource = "~/somedir/somefile_t.jpg"

var dir = Server.MapPath(filesource);


            FileInfo FileToDelete = new FileInfo(dir);


            if (FileToDelete.Exists) 
            { 
                var FileName = Path.GetFileNameWithoutExtension(FileToDelete.Name);

                foreach(FileInfo file in FileToDelete.Directory.GetFiles(FileName.Substring(0, FileName.Length - 2), SearchOption.TopDirectoryOnly).ToList())
               {
                   file.Delete();
               }

            }

DirectoryInfo.GetFiles Method (String, SearchOption) DirectoryInfo.GetFiles方法(String,SearchOption)

You need to ensure that the first parameter, searchPattern , is proper. 您需要确保第一个参数searchPattern正确。 In you're case you are supplying FileName.Substring(0, FileName.Length - 2) , which would be "somefile" . 在你的情况下,你提供FileName.Substring(0, FileName.Length - 2) ,这将是“somefile” The reason the method returns nothing is because you are looking for files literally named somefile. 该方法什么都不返回的原因是因为您正在寻找名为somefile的文件。 What you meant to do was to use a wildcard in addition to the base filename: String.Concat(FileName.Substring(0, FileName.Length - 2), "*") , which would be "somefile*" ... at least I think you're looking for that searchPattern as opposed to any other one. 你打算做的是除了基本文件名之外还使用通配符: String.Concat(FileName.Substring(0, FileName.Length - 2), "*") ,这将是“somefile *” ... at至少我认为你正在寻找那个searchPattern ,而不是任何其他的。

This code works for me: 这段代码适合我:

        var file_path = @"K:\Work\IoCToy\IoCToy\image.jpg";
        var dir = Path.GetDirectoryName(file_path);
        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file_path);
        var files = Directory.EnumerateFiles(dir, string.Format("{0}*", fileNameWithoutExtension), SearchOption.TopDirectoryOnly);

Of course, you have to delete the files by the returned file names. 当然,您必须通过返回的文件名删除文件。 I am assuming here that your folder contains only the image and the thumbnail file which start with the "image" substring. 我假设你的文件夹只包含图像和以“image”子字符串开头的缩略图文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM