服务由本地服务帐户下运行时,不是所有的过程Process.GetProcesses信息 [英] Not all info about process in Process.GetProcesses when run in service by Local Service account

查看:533
本文介绍了服务由本地服务帐户下运行时,不是所有的过程Process.GetProcesses信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code:

    [PermissionSet(SecurityAction.Assert, Name = "FullTrust")]
    public List<WinInfo> GetWindows()
    {
        try
        {
            var isFullTrust = Assembly.GetExecutingAssembly().IsFullyTrusted;
            if (isFullTrust)
            {
                return Process.GetProcesses().Where(z => !string.IsNullOrEmpty(z.MainWindowTitle))
                    .Select(z => new WinInfo
                        {
                            ProcessID = z.Id,
                            ProcessName = z.ProcessName,
                            WinID = z.MainWindowHandle,
                            WindowTitle = z.MainWindowTitle
                        }).ToList();
            }
            else
                return null;
        }
        catch (Exception ex)
        {
            Trace.Write(ex.Message);
            return null;
        }
    }

当我测试在我的本地计算机上我下的当前用户(管理员权限),它工作正常,显示所有的进程,有窗户。但是,当我把这个code,从一个窗口服务,在本地服务帐户运行,则该列表是空的。我重视的过程,并通过调试我发现,Process.GetProcesses()返回的所有进程,但个个都MainWindowHandle为0,MainWindowTitle为空,即使他们确实有窗户。那么,什么是错我的code?

When I test in on my local computer under my current user (with admin rights) it works ok, displaying all the processes, that have windows. But when I call this code from a windows service, run under "Local Service" account, then the list is empty. I attached to the process, and through debug I found that "Process.GetProcesses()" returns all the processes, but all of them have MainWindowHandle as 0 and MainWindowTitle as empty, even when they do have windows. So what is wrong with my code?

修改我编辑code,令其检查完全信任的程序集,并有PemmissionSet,应该给予Ç留点权利,$ C $。仍是结果是一样的。当我调试,我可以看到,isFullTrust是真和code执行,没有例外。仍然列表是空的,因为没有任何处理的含有不空MainWindowTitle

Edit I edited code, so that it checks the assembly for full trust and have PemmissionSet that should grant the code the neccessary rights. Still the result is the same. When I debug, I can see, that "isFullTrust" is "True" and code executes with no exceptions. Still the list is empty, because none of the processes contains not-empty MainWindowTitle

推荐答案

据<一href="http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/989405f4-ad8d-49cf-a0ef-6d92aa353e0d"相对=nofollow>这个主题:

您所看到的问题是,因为默认情况下,服务没有使用任何交互式桌面。我不建议用从服务桌面交互(#1,有可能没有任何台式机,#2可能有多个桌面,#3与在Vista服务桌面交互的未实现),但是,你可以检查在Interace与桌面,在你的服务的属性。

The problem you're seeing is because by default service don't have access to any interactive desktops. I don't recommend interacting with the desktop from a service (#1, there may not be any desktop, #2 there may be multiple desktops, #3 interacting with the desktop from service in Vista is not implemented) but, you can check the "Interace with desktop" in your services properties.

也许你可以尝试创建一个隐藏的表单?

maybe you can try to create an hidden form?

这篇关于服务由本地服务帐户下运行时,不是所有的过程Process.GetProcesses信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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