Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all files created by specifed user

Tags:

c#

ntfs

quota

I have quotas-enabled drive and I want to remove all files created by specifed user (actually a set of applications that runs using special account) from that drive. How can I do this without recursivly checking all files and folders on HDD is it created by specifed user or not? I just need to get "iterator".

like image 637
Alexus Avatar asked Oct 07 '22 23:10

Alexus


1 Answers

Take a look on 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);
    }
like image 177
GSerjo Avatar answered Oct 10 '22 01:10

GSerjo