C# – Delete all files in a directory

Use Directory.EnumerateFiles() to get the file paths in a directory. Then loop over the file paths and delete each file. Here’s an example: This deletes the root directory’s files without deleting the directory itself. I’ll show more examples. Delete all files with a specific extension Use Directory.EnumerateFiles’ searchPattern pattern to search for files with a … Read more

C# – How to delete a directory

The simplest way to delete a directory is by using Directory.Delete() (in System.IO). Here are a few examples: You have to specify the path of the directory to delete. This can be an absolute or relative path. You can pass in a recursive flag, which tells it to delete everything in the directory (files and … Read more

C# – Manually validate objects that have model validation attributes

You can use the Validator utility class to do manual attribute-based validation on any object, in any project type (as opposed to doing automatic model validation in ASP.NET). To do this, add model validation attributes to your class properties, then create an object and populate it (aka binding), and finally execute manual validation with the … Read more

C# – Unit test doesn’t finish and stops all other tests from running

Problem You have a unit test that doesn’t finish, and it prevents other tests from running. There’s no indication that the test passed or failed. It just stops running. When you run all of the tests together, some tests might finish, but once this one bad test stops, it prevents other tests from running. This … Read more