有没有更好的方法在字典中查找子字符串

时间:2012-07-10 07:10:48

标签: c# string comparison

我目前正在创建一个文件扫描程序,它根据特定条件枚举文件。其中一个扫描选项是排除大于预定义大小的文件。此规则可以应用于各个目录及其子目录。

例如,用户可以指定规则,仅从C:\ Users {USERNAME} \ Documents中获取小于1GB的文件。因此,如果用户决定扫描文档文件夹中的目录,请说:C:\Users\{USERNAME}\Documents\SOMEDIR1\SOMEDIR2\指定的规则应该应用于该目录,并且只应填充大小小于或等于1GB的文件。

目前我将规则存储在定义为Dictionary<string, long> dSizeLimit;的字典中,其中键是完整目录路径,值是规则的文件大小(以字节为单位)。

目前,我使用以下方法确定是否应从填充的文件列表中省略文件:

public void SearchDirectory(DirectoryInfo dir_info, List<string> file_list, ref long size, ScanOptions Opt = null)
        {
            if (Opt == null)
                Opt = DefaultOption;
            try
            {
                foreach (DirectoryInfo subdir_info in dir_info.GetDirectories())
                {
                    SearchDirectory(subdir_info, file_list, ref size, Opt);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Failed to enumerate directory: {0}", dir_info.FullName);
                Console.WriteLine("Exception: {0}", ex.Message);
            }

            try
            {

                foreach (FileInfo file_info in dir_info.GetFiles())
                {
                    //Here I iterate over all the size rules to determine if the current file should be added to the file_list 
                    foreach (KeyValuePair<string,long> entry in Opt.dSizeLimit)
                    {
                        if(string.Compare(entry.Key, 0, file_info.FullName, 0, entry.Key.Length, true)==0)
                        {
                            if (entry.Value > 0 && file_info.Length > entry.Value)
                                continue;
                        }
                    }

                    file_list.Add(file_info.FullName);
                    size += file_info.Length;
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Failed to enumerate directory: {0}", dir_info.FullName);
                Console.WriteLine("Exception: {0}", ex.Message);
            }
        }

ScanOptions是一个包含所有扫描规则(包括大小规则)的结构。 从代码中可以看出,我目前遍历所有规则以确定当前文件是否应包含在文件列表中。这可能是致命的,因为dSizeLimit字典中的条目数量不受限制,因为用户可以添加他想要的规则。

那么有更好的方法来处理这种查找吗?

P.S。请注意,我的目标框架应该是.NET 2.0,因此LINQ和任何其他非2.0友好的命名空间是不可能的。

1 个答案:

答案 0 :(得分:1)

如果在目录的基础上应用规则,那么您可以在迭代文件之前确定最严格的规则,如下所示:

long maxSize = long.MaxValue;
foreach (KeyValuePair<string,long> entry in Opt.dSizeLimit) {
    if(dir_info.FullName.StartsWith(entry.Key)) {
        maxSize = Math.Min(maxSize, entry.Value);
    }
}
// now iterate on the files, if no rules were present, file size
// should always be < long.MaxValue

没有理由(如果我理解正确的话)每次为同一文件夹中的文件重新扫描规则,这样可以节省大量的操作。

为了避免在字典上进行迭代,你可以让选项struct只有一个值,然后当你迭代文件夹时,用适当的值构造结构,就像这样(伪代码,只是为了给你一个想法):

foreach (DirectoryInfo subdir_info in dir_info.GetDirectories()) {
    ScanOptions optForSubFolder = Opt;
    if (/* more specific rules for given subfolder */) {
        optForSubFolder.SizeLimit = /* value for subfolder */;
    }
    SearchDirectory(subdir_info, file_list, ref size, optForSubFolder);
}