0

I want to get my specific files with Directory.GetFiles but i can't

var deleteFile = Directory.GetFiles(@"C:\test\").Where(f => f.Contains(lastBackup.DbName + " DiffBackup.bak")).ToList();
                
                    foreach (var file in deleteFile )
                    {
                        File.Delete(file);
                    }
Taylan
  • 1
  • 3
    What happens when you run the code? Do you get an exception? Is the `deleteFile` list emtpy? Can you specify a filename that actually exists in the "C:\test\" directory you expect to find with this code? It's really hard to help you without additional information. – C.Evenhuis Feb 18 '21 at 14:33
  • You've told us you "can't" but you haven't explained why not. The code looks fine to me. You also haven't actually asked us a question. What is going wrong? And what help do you need? – Enigmativity Feb 18 '21 at 22:31
  • I am sorry. I'm new here. When i run my code i get the `deleteFile` list empty. I wrote again and it's fixed. I don't know how. I just wrote. I did not change my form or app. I don't know what to do now. Should i delete my question? – Taylan Feb 19 '21 at 06:10

2 Answers2

0

Consider using this:

Directory.EnumerateFiles(@"C:\test\", lastBackup.DbName + " DiffBackup.bak").ToList();

This will look in the directory for all files with a certain extension, for example:

Directory.EnumerateFiles(@"C:\test\", "*.*").ToList();

This would return all files found in said folder.

Freek W.
  • 406
  • 5
  • 20
  • The `ToList` call [makes the enumaration pointless](https://stackoverflow.com/a/5669635/2590375) and since that is the only difference, i doubt that it solves the problem. – nilsK Feb 18 '21 at 14:43
  • You are correct, however from his example I thought `List` output since it might be more workable. – Freek W. Feb 18 '21 at 14:45
0

Maybe your path is invalid.

Try to use Path.combine() to get a good Path -> https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=net-5.0

And test if your path is correct with Directory.Exist() or File.Exist if it's a file.