0

I want create a program that finds folder sizes on my servers, but...

I have several folders where I have no access (fx Roaming User Profiles - on the server), is it possible as an administrator to get size for these folders?

I guess since the backup system can access these folders, then I should too ... somehow.

EDIT:

I see people marking my question as duplicate, but ..

  1. How can Impersonate help me? With impersonate, I run the process as another user, but which user should I run it as, to get folder sizes of all users profiles on the server (folder)?

  2. Already tried elevated UAC as in comments, that had no effect. Still getting Access Denied.

So I'm still not near a solution to my question.

MojoDK
  • 4,410
  • 10
  • 42
  • 80
  • @stuartd I tried changing manifest to ... ... still get "Access Denied". – MojoDK Feb 18 '16 at 11:21
  • It sounds like you're trying to use the Local Admin, but local Admin doesn't have rights on the remote machine. In that case you should look into impersionation: http://stackoverflow.com/questions/125341/how-do-you-do-impersonation-in-net – Dan Field Feb 18 '16 at 16:20
  • Mojo, I think you're getting close to a non-duplicate question. Maybe post a new question with your clarified problem: You need to access folders on a remote machine that the administrator of the current machine doesn't have access to. My thought is that you'd want to impersonate the admin on the remote machine, but if that's not it then you have a new question. – Dan Field Feb 18 '16 at 17:47

1 Answers1

1

If you want to continue with the next folder after a fail, then yea; you'll have to do it yourself. I would recommend a Stack (depth first) or Queue (bredth first) rather than recursion, and an iterator block (yield return); then you avoid both stack-overflow and memory usage issues.

public static IEnumerable<string> GetFiles(string root, string searchPattern)
{
    Stack<string> pending = new Stack<string>();
    pending.Push(root);
    while (pending.Count != 0)
    {
        var path = pending.Pop();
        string[] next = null;
        try
        {
            next = Directory.GetFiles(path, searchPattern);                    
        }
        catch { }
        if(next != null && next.Length != 0)
            foreach (var file in next) yield return file;
        try
        {
            next = Directory.GetDirectories(path);
            foreach (var subdir in next) pending.Push(subdir);
        }
        catch { }
    }
}

Or You can do as below:

You can set the program so you can only run as administrator.

In Visual Studio:

Right click on the Project -> Properties -> Security -> Enable ClickOnce Security Settings

After you clicked it, a file will be created under the Project's properties folder called app.manifest once this is created, you can uncheck the Enable ClickOnce Security Settings option

Open that file and change this line :

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

to:

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

This will make the program require administrator privileges, and it will guarantee you have access to that folder.

SmartestVEGA
  • 8,415
  • 26
  • 86
  • 139
  • Sry but that does't help me :) I need to get the size of the folder I can't access. – MojoDK Feb 18 '16 at 11:28
  • Nope ... Administrators doesn't have access to Roaming User Profile folder (on the server). Already tried the requestedExecutionLevel as mention in my comment above, but it didn't help :( – MojoDK Feb 18 '16 at 11:35