Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

In term of improving performance, I think you could use Task Parallel Library when using recursive algorithm to search file and folder.

Another way, you could do that Lucence was a useful framework for search and it was already published version for .NET

Actually, you can do that iteratively and very efficiently using USN Change Journal, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa363798.aspx. With proper use of filtering, you can get list of files created by specific user within specific time period.

On the other hand, this technique is quite complicated and is suitable for time-critical applications; if efficiency is not the focal point of your application, I'd choose simpler solution.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top