Get AppData \ Local folder for registered user

I am currently using:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

To get the current user path AppData\Local. The program requires elevated privileges and runs it under a standard user session, prompting an invitation that requires credentials to log into the system. Registration as an administrator (another user), apparently, changes the active user for the program. The returned folder path is thus the administrator, not the one used by the standard user.

Expected Result:

C:\Users\StandardUser\AppData\Local

Actual result:

C:\Users\Administrator\AppData\Local

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

+4
source share
1 answer

To get this information for another user, you need to know this username / password as described in this question .

So, I would like to offer an alternative solution:

1.- requestedExecutionLevel . , .

2.- .

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

WPF-, winforms.

: UAC Self Elevation

+2

Source: https://habr.com/ru/post/1682844/


All Articles