[英]Check if a process is running as Administrator/Elevated from another application - Windows 10
Is there a way to check if an existing process is already running as Administrator/Elevated from a VB .Net WinForm application?
有没有办法检查现有进程是否已经从 VB .Net WinForm 应用程序以管理员/提升身份运行? I have a utility which connects to the Main application via API.
我有一个通过 API 连接到主应用程序的实用程序。 This was all working fine on Windows 7 but on Windows 10, my utility was unable to connect to the main program via API.
这在 Windows 7 上一切正常,但在 Windows 10 上,我的实用程序无法通过 API 连接到主程序。 I tired lots of things but nothing worked.
我厌倦了很多事情,但没有任何效果。 Finally I found that if I run the main program As Administrator, the my API utility worked/connected just like that.
最后我发现如果我以管理员身份运行主程序,我的 API 实用程序就像那样工作/连接。 I reviewed the documentation for Process class ( https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=netframework-4.7.2 ) but can't find anything obvious to elevated execution property.
我查看了 Process 类的文档( https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=netframework-4.7.2 )但找不到任何明显的提升执行财产。 Any advise or code snippet would be appreciated.
任何建议或代码片段将不胜感激。
In Windows 10 Task Manager, you can see if the process is running as Elevated or Not.在 Windows 10 任务管理器中,您可以查看进程是否以 Elevated 运行。 I just need something to capture this in the code.
我只需要一些东西来在代码中捕获它。
After some testing around I found that after adding app.manifest file with below settings and adding a property IsElevated in the code, I could see if the exe is running as Elevated or not in Task manager.经过一些测试,我发现在添加具有以下设置的 app.manifest 文件并在代码中添加属性 IsElevated 后,我可以查看 exe 是否在任务管理器中以 Elevated 运行。 Hope this can help anyone looking for similar matter.
希望这可以帮助任何寻找类似问题的人。
from app.manifest file:来自 app.manifest 文件:
<security> <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> <!-- UAC Manifest Options If you want to change the Windows User Account Control level replace the requestedExecutionLevel node with one of the following. <requestedExecutionLevel level="asInvoker" uiAccess="false" /> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> Specifying requestedExecutionLevel element will disable file and registry virtualization. Remove this element if your application requires this virtualization for backwards compatibility. --> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> </security>
Public ReadOnly Property IsElevated As Boolean
Get
Return New WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator)
End Get
End Property
When using in the code:在代码中使用时:
If IsElevated = True Then
Me.Text = "TEST Utility - Administrator (Elevated)"
End If
If IsElevated = False Then
Me.Text = "TEST Utility - Normal (Non-Elevated)"
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.