Delete all files created by the specified user.

I have a drive with quota support, and I want to delete all files created by the specified user from it (in fact, a set of applications that run using a special account). How can I do this without recursively checking all the files and folders on the hard drive created by the user or not? I just need to get an "iterator".

+5
source share
3 answers

Take a look at the following example

    [Test]
    public void Test()
    {
        string user = @"Domain\UserName";
        var files = Directory.EnumerateFiles(@"C:\TestFolder")
            .Where(x => IsOwner(x, user));
        Parallel.ForEach(files, File.Delete);
    }

    private static bool IsOwner(string filePath, string user)
    {
        return string.Equals(File.GetAccessControl(filePath).GetOwner(typeof (NTAccount)).Value, user,
                             StringComparison.OrdinalIgnoreCase);
    }
+2
source

In terms of improving performance, I think you could use a parallel task library when using a recursive algorithm to search for a file and folder.

, Lucence , .NET.

+1

All Articles