简体   繁体   English

名单 <string> .contains with wildcards c#

[英]List<string>.contains with wildcards c#

I'm trying to match a file name to a partial string in a List. 我正在尝试将文件名匹配到List中的部分字符串。 The list will have something like 192 in it and the matching file will be xxx192.dat. 该列表中将包含192个匹配文件,匹配文件为xxx192.dat。 Using .Contains does not match the file name to the string in the List. 使用.Contains将文件名与List中的字符串不匹配。 Can anyone tell me how to get this done or how to use wildcard chars in the contains? 谁能告诉我如何完成这项工作或如何在包含中使用通配符? Code below. 代码如下。

    // use this to get a specific list of files
    private List<string> getFiles(string path, List<string> filenames)
    {
        List<string> temp = new List<string>();
        string mapPath = Server.MapPath(path);
        //DirectoryInfo Di = new DirectoryInfo(mapPath);
        DirectoryInfo Di = new DirectoryInfo(@"C:\inetpub\wwwroot\Distribution\" + path); // for testing locally
        FileInfo[] Fi = Di.GetFiles();

        foreach (FileInfo f in Fi)
        {
            if (filenames.Contains(f.Name)) **// *** this never matches**
             temp.Add(f.FullName);
        }
        return temp;
    }

I'v changed the code trying to use the suggestions but it's still not working. 我改变了试图使用建议的代码,但它仍然无法正常工作。 I'll add in the data like I'm stepping through the code. 我将添加数据,就像我正在逐步完成代码。

    // use this to get a specific list of files
    private List<string> getFiles(string path, List<string> filenames)
    {
        List<string> temp = new List<string>();
        string mapPath = Server.MapPath(path);
        //DirectoryInfo Di = new DirectoryInfo(mapPath);
        DirectoryInfo Di = new DirectoryInfo(@"C:\inetpub\wwwroot\Distribution\" + path); // for testing locally

        foreach (string s in filenames) // list has 228,91,151,184 in it
        {
            FileInfo[] Fi = Di.GetFiles(s); // s = 228: Fi = {System.IO.FileInfo[0]}
            foreach (FileInfo f in Fi) //Fi = {System.IO.FileInfo[0]}
            {
                temp.Add(f.FullName);
            }
        }

        return temp;
    }

When I look at the directory where these files are I can see: 当我查看这些文件所在的目录时,我可以看到:

pbset228.dat pbset228.dat

pbmrc228.dat pbmrc228.dat

pbput228.dat pbput228.dat

pbext228.dat pbext228.dat

pbget228.dat pbget228.dat

pbmsg228.dat pbmsg228.dat

This is working now. 现在正在运作。 It may not be the most efficient way to do this, but it gets the job done. 它可能不是最有效的方法,但它完成了工作。 Maybe someone can post a sample that does the same thing in a better way. 也许有人可以发布一个以更好的方式做同样事情的样本。

    // use this to get a specific list of files
    private List<string> getFiles(string path, List<string> filenames)
    {
        List<string> temp = new List<string>();
        string mapPath = Server.MapPath(path);
        //DirectoryInfo Di = new DirectoryInfo(mapPath);
        DirectoryInfo Di = new DirectoryInfo(@"C:\inetpub\wwwroot\Distribution\" + path); // for testing locally
        FileInfo[] Fi = Di.GetFiles();

        foreach (FileInfo f in Fi)
        {
            foreach (string s in filenames)
            {
                if (f.Name.Contains(s))
                    temp.Add(f.FullName);
            }               
        }

        return temp;
    }

You can use the Any() LINQ extension: 您可以使用Any() LINQ扩展:

filenames.Any(s => s.EndsWith(f.Name));

This will return True if any element in the enumeration returns true for the given function. 如果枚举中的任何元素对给定函数返回true,则返回True

For anything more complex, you could use a regular expression to match: 对于任何更复杂的东西,您可以使用正则表达式来匹配:

filenames.Any(s => Regex.IsMatch(s, "pattern"));

Use the static Directory.GetFiles method that lets you include a wildcards and will be more efficient that retrieving all the files and then having to iterate through them. 使用静态Directory.GetFiles方法,该方法允许您包含通配符,并且将更有效地检索所有文件,然后必须迭代它们。

Or you can even use DirectoryInfo.GetFiles and pass your search string to that. 或者您甚至可以使用DirectoryInfo.GetFiles并将搜索字符串传递给它。

Change this 改变这个

foreach (FileInfo f in Fi)
{
    if (filenames.Contains(f.Name)) **// *** this never matches**
    temp.Add(f.FullName);
}
return temp;

Into something like this 变成这样的事情

temp = filenames.Find(file => file.Contains(someNameYoureLookingFor));

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

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