如何使用.NET从多用户环境中的Windows Service获取当前Windows用户名 [英] How to get current windows username from windows service in multiuser environment using .NET

查看:48
本文介绍了如何使用.NET从多用户环境中的Windows Service获取当前Windows用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,该服务始终为所有登录的用户运行WPF应用程序,并且运行良好,现在在WPF应用程序中,我无法获得当前的用户名Environment.UserName;返回"SYSTEM",这是可以理解的.所以我想是找到可以由Process.GetCurrentProcess().SessionId检索的当前进程的会话ID,然后获取登录到该计算机并循环遍历的所有用户的列表,以找到与进程会话匹配的会话IDid和后来的用户名.

I have a windows service which run a WPF application all the time for all the logged in users which works fine, now in the WPF application i can not get a current username as Environment.UserName; returns 'SYSTEM' which is understandable. so what i thought was to find session id of current process which could be retrieved by Process.GetCurrentProcess().SessionId and then get the list of all users logged in to the machine and looping through it to find the session id match with process session id and later his username.

但是我不知道如何获得所有已登录用户的列表,或者如果有人可以帮助我,我将不胜感激.

but i don't how to get the list of all logged in users or i would appreciate if someone can help me with alternative.

推荐答案

我通过在WPF应用程序中执行powershell命令"quser"来解决该问题,该命令返回所有已登录的用户,然后迭代查找该应用程序所在的会话ID正在使用用户会话ID运行,然后检索其名称.下面是通过传递用户名的会话ID来获取用户名的功能

I solve it by executing powershell command "quser" in my WPF application which returns all the logged in users then I am iterating to find session id in which the application is running with user session id and then retrieving his name. below is the function which fetch the username by passing his session id

 private string GetUserName(int SessionId)
        {
            try
            {
                Runspace runspace = RunspaceFactory.CreateRunspace();
                runspace.Open();

                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript("Quser");
                pipeline.Commands.Add("Out-String");

                Collection<PSObject> results = pipeline.Invoke();

                runspace.Close();

                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject obj in results)
                {
                    stringBuilder.AppendLine(obj.ToString());
                }

                foreach (string User in stringBuilder.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Skip(1))
                {
                    string[] UserAttributes = User.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries);
                    if (int.Parse(UserAttributes[2].Trim()) == SessionId)
                    {
                        return UserAttributes[0].Replace(">", string.Empty).Trim();
                    }
                }

                return stringBuilder.ToString();
            }
            catch (Exception ex)
            {
            }

            return string.Empty;
        }

该函数可以通过

string CurrentUser = GetUserName(Process.GetCurrentProcess().SessionId);

这篇关于如何使用.NET从多用户环境中的Windows Service获取当前Windows用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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