开发者

How to run my app with privileges for access to _every_ directory?

开发者 https://www.devze.com 2023-02-28 14:01 出处:网络
I am working on a small backup application for my personal use, and as part of it I wo开发者_如何学Pythonuld like to iterate through every directory on a given disk.

I am working on a small backup application for my personal use, and as part of it I wo开发者_如何学Pythonuld like to iterate through every directory on a given disk.

I have set my app to run with administrator privileges using the manifest and while methods like GetDirectories() can now enter system folders they still fail with an UnauthorizedAccessException when accessing Documents and Settings.

I will (have to) filter out system files/open files etc, of course, but for now just generating the list I want to give it access to the entire hard drive, but neither the 'requireAdministrator' or 'highestAvailable' requestedExecutionLevel appear to do that; how can I give it access to the entire drive?


You'll need to have FileIOPermissionAccess.AllAccess permission. For that, you'll need to login as an administrator. If you cannot do that, your best change is to just skip the folders. Below sample is from a similar question:

try
{
    Directory.GetFiles(path)
        .ToList()
        .ForEach(s => files.Add(s));

    Directory.GetDirectories(path)
        .ToList()
        .ForEach(s => AddFiles(s, files));
}
catch (UnauthorizedAccessException ex)
{
    // ok, so we are not allowed to dig into that directory. Move on.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号