Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've an array. Let's say;

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


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


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.

C#
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);
                    }
                }
            }
        }
Posted
Updated 15-May-17 22:18pm

1 solution

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:
C#
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.
 
Share this answer
 
Comments
Sai Parthiv 16-May-17 5:07am    
Thank you, How do I replace it in my code? Like I have to filter out files with multiple file extensions.
OriginalGriff 16-May-17 5:14am    
Create a loop which checks the "banned extensions" array against the values returned by the GetExtension method.
Maciej Los 16-May-17 10:57am    
OMG changes into OG! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900