1

I Need to get users special folders size and remove unnecessary files. I have Administrator access, but in the first place i receive access denied exception. my code is

        static void Main(string[] args)
    {
        var basePath = @"C:\Users";

        DirectoryInfo basePathInfo = new DirectoryInfo(basePath);
        var usersDirectories = basePathInfo.GetDirectories();

        foreach (var directory in usersDirectories)
        {
            foreach (var dir in directory.GetDirectories())
            {
               Console.WriteLine($"{dir} : {DirSize(dir)} Bytes");
            }
        }

        Console.ReadLine();
    }

    public static long DirSize(DirectoryInfo d)
    {
        // Add file sizes.
        FileInfo[] fis = d.GetFiles();
        long size = fis.Sum(fi => fi.Length);

        // Add subdirectory sizes.
        DirectoryInfo[] dis = d.GetDirectories();
        size += dis.Sum(di => DirSize(di));

        return size;
    }

i tried some folders and found out that i cant read data form below folders

AppData

Application Data

Cookies

Documents

Local Settings

My Documents

NetHood

PrintHood

Recent

SendTo

Start Menu

Templates

I Also Added a manifest file and use this tag for run as administrator

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Please help, what can I do?

Thanks for your Time.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Is your application running `As Administrator`? – Samvel Petrosov Jul 12 '17 at 05:39
  • yes i try it too. also i added a manifest file and use this tag – Mohammad Haji Hosseini Jul 12 '17 at 06:20
  • Are you working in a Cooperate Network. There are different admin account that are controlled by Group Policies which may prevent you from accessing folders. Can you access these folder using a Windows Explorer? If you can then the executable is not running with Admin Privileges. The default is using your normal credentials. – jdweng Jul 12 '17 at 06:35
  • I Tested the above code in these conditions: my local account on my pc, administrator account on my pc, my account on domain, administrator on server. all of these modes made exception access denied – Mohammad Haji Hosseini Jul 12 '17 at 09:25
  • Admin privileges don't mean, you have rights to read everything, but you can get them. So first check if you have read access, if not get it. – BitAccesser Jul 13 '17 at 21:51

0 Answers0