获取登录用户的AppData\Local文件夹 [英] Get AppData\Local folder for logged user

查看:1283
本文介绍了获取登录用户的AppData\Local文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

检索当前用户的 AppData\Local 路径。该程序需要提升的特权,并且在标准用户会话下运行该程序会提示您输入管理员的登录凭据。以管理员身份(不同用户)登录显然会更改程序的活动用户。因此,返回的文件夹路径是管理员的文件夹路径,而不是标准用户使用的文件夹路径。

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.

预期结果:

C:\Users\StandardUser\AppData\Local

实际结果:

C:\Users\Administrator\AppData\Local

是否可以获取特定用户的AppData\Local路径?与获取任意用户的路径相比,获取登录的用户名或凭据不是问题。该应用程序基于WPF,其必需的权限由 requestedEcecutionLevel(requireAdministrator)在清单文件中设置。

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).

推荐答案

要获取其他用户的信息,您需要知道该用户名/密码,如这个问题

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.-而不是使用 requestedExecutionLevel 进行应用,将其删除并以登录用户身份运行。这样一来,您将可以轻松访问特殊文件夹路径,并可以对其进行记录。

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。-以管理员身份重新启动应用程序。

2.- Restart your application as Administrator.

示例代码(在App.xaml.cs中):

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

此示例代码用于WPF应用程序,但可以完成

参考: UAC自我提升

这篇关于获取登录用户的AppData\Local文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆