문제

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

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top