VC# 怎样获取系统用户名,图片和用户类型
我要写三个 click 事件,来显示三个信息,用 GetUserName(); 显示错误。应该怎么写才适合XP,VISTA 和WIN7系统显示这三个系统用户信息。。代码如下。。我应该怎样写?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CC
{
public partial class Home : Form
{
public Home()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//show user account picture
}
private void label1_Click(object sender, EventArgs e)
{
//show user account Name
}
private void label2_Click(object sender, EventArgs e)
{
// show user account Type
}
}
}
--------------------编程问答-------------------- 没看明白 --------------------编程问答-------------------- 就是你按电脑的“开始”菜单的时候,你会看到一个小图片,还有当前的用户名,和该用户的类型,即是系统管理员,还是超级用户,我现在想在窗口的 pictureBox1显示那个小图片,label1直接显示用户名,label2显示该用户的类型。 --------------------编程问答-------------------- 自己顶起。。求高手指点。 --------------------编程问答-------------------- 1.获得当前用户名:
using System.Management;
SelectQuery query = new SelectQuery("select * from Win32_UserAccount where Name='" + System.Environment.UserName + "'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject envVar in searcher.Get())
{
string fullName = envVar["FullName"].ToString();
}
2。获得当前用户权限(参考了 http://pcheruku.wordpress.com/2008/07/31/sample-c-code-to-get-user-login-type/)
using System.Security.Principal;
WindowsIdentity ident = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(ident);
string userType = "";
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
{
userType = "Administrator";
}
else if (principal.IsInRole(WindowsBuiltInRole.PowerUser))
{
userType = "Power User";
}
else if (principal.IsInRole(WindowsBuiltInRole.User))
{
userType = "User";
}
else if (principal.IsInRole(WindowsBuiltInRole.Guest))
{
userType = "Guest";
}
3.获取用户图片,参考http://go4answers.webhost4life.com/Example/get-windows-user-account-name-picture-77793.aspx
--------------------编程问答-------------------- 如果是数据访问的话建议楼主看看三层架构的类库设计 --------------------编程问答-------------------- 不好意思,回复错了 --------------------编程问答--------------------
string userName = System.Environment.UserName;
补充:.NET技术 , C#