您的位置:首页 > 其它

取得Window系统的当前用户

2012-04-23 14:30 162 查看
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace WindowsApplication25

{

public partial class Form1 : Form

{

[DllImport( "Advapi32.dll ", EntryPoint = "GetUserName ",

ExactSpelling = false, SetLastError = true)]

static extern bool GetUserName(

[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,

[MarshalAs(UnmanagedType.LPArray)] Int32[] nSize);

public Form1()

{

InitializeComponent();

System.Text.StringBuilder b = new System.Text.StringBuilder(100);

int n = b.Capacity;

byte[] str = new byte[256];

Int32[] len = new Int32[1];

len[0] = 256;

GetUserName(str, len);

MessageBox.Show(System.Text.Encoding.ASCII.GetString(str));

string a;

a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

MessageBox.Show(a.ToString());

}

}

}

The GetUserName function retrieves the name of the user associated with the current thread.(MSDN)

http://topic.csdn.net/u/20070528/15/46630238-b6a0-4d3e-9c6d-a501ac2588e4.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐