5

I'm currently using:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

To retrieve current user's AppData\Local path. The program requires elevated privileges and running it under standard user session throws a prompt requiring administrator's login credentials. Logging as an administrator (different user) apparently changes active user for the program. The returned folder path is thus administrator's and not the one the standard user uses.

Expected result:

C:\Users\StandardUser\AppData\Local

Actual result:

C:\Users\Administrator\AppData\Local

Is there a way to get AppData\Local path of specific user? Getting logged user name or credentials is not an issue compared to getting the path for arbitrary user. The application is WPF based and its required privileges are set in manifest file by requestedEcecutionLevel (requireAdministrator).

Vilda
  • 1,675
  • 1
  • 20
  • 50
  • Why is this program needing admin rights? It is not "the best solution" - to grant admin right to all programs (it is workaround) – Piotr Aug 02 '17 at 10:51
  • @Piotr It is an installer program and administrative privileges are required (writing to registry, Program Files, etc..) – Vilda Aug 02 '17 at 10:52
  • @Pikoh This question relates to how to get AppData\Local path of specific user, it does not deal with getting current user name, signature or credentials. – Vilda Aug 02 '17 at 11:02
  • In your question you say _for logged user_. So you need to know what is the logged user from an application running as administrator. And that's what the possible duplicate deals with if i'm not wrong – Pikoh Aug 02 '17 at 11:04
  • @Pikoh True, but getting the logged user signature is not that difficult compared to getting AppData\Local path for arbitrary user. – Vilda Aug 02 '17 at 11:05
  • 1
    You are right. Anyway, it seems you can't do what you want without knowing the username/password(see [this question](https://stackoverflow.com/q/37630726/579895) )... – Pikoh Aug 02 '17 at 11:31
  • see that - https://stackoverflow.com/questions/2779343/is-it-possible-to-install-into-program-files-with-limited-privileges – GCamel Aug 02 '17 at 12:31

1 Answers1

2

To get that information for another user, you'll need to know that user username/password, as is explained in this question.

So I'd like to throw an alternative solution:

1.- Instead of using the requestedExecutionLevel for the aplication, remove it and run it as the logged user. That way you'll have access to the special folders path easily and may log it.

2.- Restart your application as Administrator.

Sample code (in App.xaml.cs):

private void Application_Startup(object sender, StartupEventArgs e)
{
    if (!IsRunAsAdmin())
    {
        // here you should log the special folder path 
        MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
        // Launch itself as administrator 
        ProcessStartInfo proc = new ProcessStartInfo();
        proc.UseShellExecute = true;
        proc.WorkingDirectory = Environment.CurrentDirectory;
        proc.FileName = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
        proc.Verb = "runas";

        try
        {
            Process.Start(proc);
        }
        catch
        {
            // The user refused the elevation. 
            // Do nothing and return directly ... 
            return;
        }

        System.Windows.Application.Current.Shutdown();  // Quit itself 
    }
    else
    {
        MessageBox.Show("The process is running as administrator", "UAC");
    }
}

internal bool IsRunAsAdmin()
{
    WindowsIdentity id = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(id);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

This sample code is for a WPF Application, but could be done the same in a winforms Application.

Reference: UAC Self Elevation

Pikoh
  • 7,582
  • 28
  • 53
  • Not the most elegant way, but it works. Clever solution, thanks! – Vilda Aug 02 '17 at 14:38
  • hello! sorry if this is a dumb question, but will this work on windows service? thank you! – jgmdv May 08 '18 at 16:07
  • @jgmdv never tried it, but it may work...The problem may be restarting itself. See [this question](https://stackoverflow.com/q/220382/579895).If you try it please keep me informed :) – Pikoh May 08 '18 at 16:13