首页 > 代码库 > C# Console Application 默认使用 admin运行
C# Console Application 默认使用 admin运行
class Program { static void Main(string[] args) { System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判断当前登录用户是否为管理员 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { DataAccess test = new DataAccess(); test.ExecuteExtract(); } else { //创建启动对象 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.WorkingDirectory = Environment.CurrentDirectory; startInfo.FileName = Assembly.GetExecutingAssembly().Location; //设置启动动作,确保以管理员身份运行 startInfo.Verb = "runas"; try { Process.Start(startInfo); } catch { return; } } } }
C# Console Application 默认使用 admin运行
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。