.NET 取得 windows 帳號資訊
取得登入帳號 Environment.UserName 取得帳號等有儲存在本機上的資訊 using System.Security.Principal; using System.Threading; //—– AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal; WindowsIdentity myIdentity = (WindowsIdentity)myPrincipal.Identity; Console.WriteLine(“IdentityType: ” + myIdentity.ToString()); Console.WriteLine(“Name: {0}”, myIdentity.Name); Console.WriteLine(“Member of Users? {0}”, myPrincipal.IsInRole(WindowsBuiltInRole.User)); Console.WriteLine(“Member of Administrators? {0}”, myPrincipal.IsInRole(WindowsBuiltInRole.Administrator)); Console.WriteLine(“Authenticated: {0}”, myIdentity.IsAuthenticated); Console.WriteLine(“Anonymous: {0}”, myIdentity.IsAnonymous); 取得帳號等有儲存在本機上的資訊 – II 使用 Win32 API using System.Runtime.InteropServices; //—– [DllImport(“Advapi32.dll”, EntryPoint=”GetUserName”, ExactSpelling=false, SetLastError=true)] static extern […]