Delete all in folder except the filename in list

Gab*_*boO 3 c# directory getfiles delete-file

I have a folder, and a list (which contain file names). I want the program delete files except the file which are listed. C#

I hope it is possible.

Now used code:

It delete only ONE file.

        string folder = Directory.GetCurrentDirectory();
        string thisNOdelete = "example.exe";
        string[] list = Directory.GetFiles(@folder);//
        foreach (string file in list)
        {

            if (file.ToUpper().Contains(thisNOdelete.ToUpper()))
            {
                    //if found this do nothing

            }
            else
            {

               File.Delete(file);
            }
        }
Run Code Online (Sandbox Code Playgroud)

Mat*_*t J 6

You could try,

    public void DeleteFilesExcept(string directory,List<string> excludes)
    {
        var files = System.IO.Directory.GetFiles(directory).Where(x=>!excludes.Contains(System.IO.Path.GetFileName(x)));
        foreach (var file in files)
        {
            System.IO.File.Delete(file);
        }
    }
Run Code Online (Sandbox Code Playgroud)