如何在C#中从搜索中排除数组 [英] How to exclude an array from search in C#

查看:92
本文介绍了如何在C#中从搜索中排除数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个阵列。让我们说吧;



I've an array. Let's say;

private string[] WhiteList =
     {
        "abcxyz.cs",
        "mouseapi.dll",
        "SensitivtyFix.asi",
        "drake.mp3"
     };







private string[] SuspectedFiles =
        {
            ".cs",
            "d3d9.dll",
            "sqlite3.dll",
            "mod_sa.ini",
            "mod_sa.raw",
            ".cleo",
            ".asi"
        };





现在我想从目录/文件搜索中排除该数组。我如何实现这一目标?



对于这个搜索,我正在使用另一个名为SuspectedFiles的数组,我可以从中获取所需的文件,但我无法从中排除文件阵列白名单。







任何帮助都会很棒。谢谢。



我尝试了什么:



这是我的我用过的代码并没有成功。我可以从SuspectedFiles数组中获取文件的结果,但无法从搜索结果中删除白名单数组中包含的文件。





Now I want to exclude that array from a directory/file search. How do I achieve that?

For this search, I'm using another array called SuspectedFiles from which I could fetch desired files but I'm unable to exclude files from the array Whitelist.



Any help would be great. Thank You.

What I have tried:

This is my code that I've used and got no success. I could get results of files from SuspectedFiles array but couldn't remove files contained in Whitelist array from the search results.

private void VerifySuspectedFiles()
        {
            listBox1.Enabled = true;

            DirectoryInfo DirInf = new DirectoryInfo(GetGtaRegistryPath());
            int GetSize = SuspectedFiles.GetLength(0);

            if (!DirInf.Exists)
            {
                MessageBox.Show("This directory does not exist!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }

            if (GetSize == 0)
            {
                return;
            }

            for (int i = 0; i < GetSize; ++i)
            {
                foreach (var File in DirInf.GetFiles("*", SearchOption.AllDirectories))
                {
                    if (File.FullName.Contains(SuspectedFiles[i]) && !File.FullName.Contains(WhiteList[i]))

                    {
                        listBox1.Items.Add("File: " + File.FullName);
                    }
                }
            }
        }

推荐答案

对于初学者,你可能想要更多地考虑你要检查的内容:可疑文件的包含测试将起作用 - 但是容易出现误报 - 但使用Contains的白名单将删除它不应该的正面匹配。例如,如果bad file被称为VirusInfectedmouseapi.dll,它将通过您的白名单,因为它包含mouseapi.dll。



我是改变它:通过所有方式有一个确定文件和坏文件列表,但也有一个坏扩展列表,并使用路径功能提取文件名和文件扩展名进行检查:

For starters, You probably want to think a bit more about what you are checking for: the Contains test for Suspected Files will work - but is liable to false positives - but the white list using Contains is going to remove positive matches that it shouldn't. For example, if the "bad file" is called "VirusInfectedmouseapi.dll" it will pass your white list because it contains "mouseapi.dll".

I'd change it about: by all means have a list of "OK files" and "bad files" but also have a list of "bad extensions" and use the Path functions to extract the file name and the file extension for checking:
string filenameWithExtension = Path.GetFileName(File.FullName);
string FileNameWithoutExtension = Path.GetFileNameWitoutExtension(File.FullName);
string extensionWithDot = Path.GetExtension(File.FullName);



然后你可以做相等匹配而不是包含,并减少您可能遇到的问题的数量。

我还会在检查之前将所有文件名转换为小写 - 系统不关心文件名中的大小写,但默认情况下字符串比较。


You can then do equality matching instead of Contains, and reduce the number of problems you might get.
I'd also convert all filenames to lowercase before I check them - the system doesn't care about case in filenames but string comparisons do, by default.


这篇关于如何在C#中从搜索中排除数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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