net环境下如果想得到当前的用户名称在可以使用GetUserName函数来实现它
下边是源代码
详细的说明请看http://dev.csdn.net/article/73/73726.shtm
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using System.Runtime.InteropServices;
namespace user
{
class Program
{
[DllImport("advapi32.dll")]
static extern int GetUserName(StringBuilder text, ref int count);
static void
{
StringBuilder name = new StringBuilder(255);
int i = 255;
GetUserName(name, ref i);
Console.WriteLine(name);
Console.WriteLine(i.ToString());
}
}
}
注意:该程序调用GetUserName API函数,打印当前用户名。注意这里应该用StringBuilder,因为在GetUserName里要改变name值(实际上是传址),而.Net 内建的类型string已经初始化,不可改变,即使传址方式也如此。而StringBuilder就是string的弥补,ref int count也是如此!
浙公网安备 33010602011771号