Search code examples
c#ntfsquota

Remove all files created by specifed user


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".


Solution

  • 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);
        }