gpt4 book ai didi

c# - 当本地服务帐户在服务中运行时,并非有关 Process.GetProcesses 中进程的所有信息

转载 作者:行者123 更新时间:2023-12-05 05:28:22 25 4
gpt4 key购买 nike

我有这个代码:

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

当我在当前用户(具有管理员权限)下在我的本地计算机上进行测试时,它工作正常,显示所有具有窗口的进程。但是当我从 Windows 服务调用此代码时,在“本地服务”帐户下运行,然后列表为空。我附加到进程,通过调试我发现“Process.GetProcesses()”返回所有进程,但所有进程的 MainWindowHandle 为 0,MainWindowTitle 为空,即使它们有窗口。那么我的代码有什么问题呢?

编辑 我编辑了代码,以便它检查程序集是否完全信任并拥有应该授予代码必要权限的 PemmissionSet。结果还是一样。当我调试时,我可以看到“isFullTrust”为“True”并且代码执行无一异常(exception)。列表仍然是空的,因为没有进程包含非空 MainWindowTitle

最佳答案

根据 this线程:

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.

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

关于c# - 当本地服务帐户在服务中运行时,并非有关 Process.GetProcesses 中进程的所有信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11504441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com