Console.WriteLine(Environment.UserName); //计算机NetBIOS名称 Console.WriteLine(Environment.MachineName); //计算机网络域名称 Console.WriteLine(Environment.UserDomainName);
判断利用WindowsPrincipal来判断用户是否具有管理员权限,运行后用户账户控制会提示是否给予程序管理员权限。
using System; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using System.Security.Principal; namespace Mgen.TTC { class Program { static void Main() { WindowsPrincipal winPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); bool admin = winPrincipal.IsInRole(WindowsBuiltInRole.Administrator); if (!admin) { if (!RunUAC(Process.GetCurrentProcess().MainModule.FileName)) { Console.WriteLine("不是管理员"); return; } } Console.WriteLine("是管理员"); } static bool RunUAC(string fileName) { ProcessStartInfo processInfo = new ProcessStartInfo(); processInfo.Verb = "runas"; processInfo.FileName = fileName; try { Process.Start(processInfo); return true; } catch (System.ComponentModel.Win32Exception) { } return false; } } }