我有这个代码:
[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/
相关文章:
python - 在 PyGame (SDL) 中使用 Cairo 时的字节顺序
hibernate - JPA/JPQL : AS identifier disallowed in
ruby-on-rails-3 - 将生成的图像文件保存为回形针附件
eclipse - 如何将当前 Eclipse 文本编辑器光标位置传递给外部工具?
python - 使用 python 需求文件,您可以控制安装包依赖项的顺序吗?
wix - 如何在不调用安装的情况下执行 MSI 中存在的自定义操作?
regex - 基于预定义文本字符串的复杂正则表达式来阻止色情
xml - 有没有比 <!ENTITY> 更好的方法来重用 DocBook 中的复杂表格?