简体   繁体   English

C#服务:如何获取用户配置文件文件夹路径

[英]c# service: how to get user profile folder path

I need to get the user directory from within a C# windows service... 我需要从C#Windows服务中获取用户目录...
...like C:\\Users\\myusername\\ ...就像C:\\ Users \\ myusername \\
Ideally, I'd like to have the roaming path... 理想情况下,我想要漫游路径...
...like C:\\Users\\myusername\\AppData\\Roaming\\ ...就像C:\\ Users \\ myusername \\ AppData \\ Roaming \\
When I used the following in a console program I got the correct user directory... 当我在控制台程序中使用以下命令时,我得到了正确的用户目录...

System.Environment.GetEnvironmentVariable("USERPROFILE"); 

...but when I use that same variable in a service, I get... ...但是当我在服务中使用相同的变量时,我得到...
C:\\WINDOWS\\system32\\config\\systemprofile C:\\ WINDOWS \\ system32 \\设置\\ systemprofile
How can I get the user folder and maybe even the roaming folder location from a service? 如何从服务中获取用户文件夹甚至漫游文件夹位置?
Thanks in advance. 提前致谢。

A service doesn't log on like a user, unless the service is configured to use a specific user's profile. 除非将服务配置为使用特定用户的配置文件,否则服务不会像用户那样登录。 So it's not going to point to "user" folders. 因此,它不会指向“用户”文件夹。

First, you'll want to use Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) 首先,您将要使用Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

Environment.SpecialFolder.ApplicationData is for roaming profiles. Environment.SpecialFolder.ApplicationData用于漫游配置文件。

Find all SpecialFolder enumeration values here: https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx 在此处查找所有SpecialFolder枚举值: https ://msdn.microsoft.com/zh-cn/library/system.environment.specialfolder( v= vs.110).aspx

As others have noted, the Service will run under the account LocalSystem/LocalService/NetworkService, depending on configuration: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686005(v=vs.85).aspx 如其他人所述,该服务将在帐户LocalSystem / LocalService / NetworkService下运行,具体取决于配置: https : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms686005(v=vs.85)的.aspx

I have searched for getting the profile path of user from Windows service. 我已经搜索了从Windows服务获取用户的配置文件路径。 I have found this question, which does not include a way to do it. 我发现了这个问题,其中没有解决问题的方法。 As I have found the solution, partly based on a comment by Xavier J on his answer, I have decided to post it here for others. 当我找到解决方案时,部分基于Xavier J对他的回答的评论,因此我决定将其发布在这里。

Following is a piece of code to do that. 以下是一段代码来做到这一点。 I have tested it on few systems, and it should work on different OSes ranging from Windows XP to Windows 10 1903. 我已经在几个系统上对其进行了测试,并且它应该可以在从Windows XP到Windows 10 1903的不同操作系统上运行。


    //You can either provide User name or SID
    public string GetUserProfilePath(string userName, string userSID = null)
    {
        try
        {
            if (userSID == null)
            {
                userSID = GetUserSID(userName);
            }

            var keyPath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" + userSID;

            var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(keyPath);
            if (key == null)
            {
                //handle error
                return null;
            }

            var profilePath = key.GetValue("ProfileImagePath") as string;

            return profilePath;
        }
        catch
        {
            //handle exception
            return null;
        }
    }

    public string GetUserSID(string userName)
    {
        try
        {
            NTAccount f = new NTAccount(userName);
            SecurityIdentifier s = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
            return s.ToString();
        }
        catch
        {
            return null;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM