本文介绍了如何以管理员身份运行代码,但“有些正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序以管理员身份运行,但无法正确使用某些代码,因为它们需要在普通用户中运行,如何切换它
我尝试过:
my Application run as administrator, but can't correctly with some code, Because they need to run in normal user, How Can Switch it
What I have tried:
private bool runNormal()
{
try
{
//Do Somthing normal , if run by admin it dosen't work
return true;
}
catch { return false; }
}
private bool runAdmin()
{
try
{
//Do Somthing advance, if run by NormalUser it dosen't work
return true;
}
catch { return false; }
}
public void StartMainProcess()
{
if (runNormal() && runAdmin())
{
//continue to next step
}
}
推荐答案
using System.Security.Principal;
public static bool IsAdmin()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
恕我直言,你应该将管理代码分成不同的DLL并手动加载它 - 如果 - 并且只有 - 用户是管理员角色。
如何为不同的用户角色编组代码是我留给你的一个练习。
IMHO, you should separate the admin code into a different DLL and manually load it if - and only if - the user is in the admin role.
How you marshal the code for the different user roles is an exercise I leave to you.
这篇关于如何以管理员身份运行代码,但“有些正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!