小编Gab*_*boO的帖子

Delete all in folder except the filename in list

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)

c# directory getfiles delete-file

3
推荐指数
1
解决办法
1298
查看次数

标签 统计

c# ×1

delete-file ×1

directory ×1

getfiles ×1