GetUserName (advapi32)
Last changed: -122.170.55.192

.
Summary
The GetUserName function retrieves the name of the user associated with the current thread.

C# Signature:

[DllImport("advapi32.dll", SetLastError = true)] static extern bool GetUserName(System.Text.StringBuilder sb, ref Int32 length)

VB Signature:

Declare Auto Function GetUserName Lib "advapi32.dll" (ByVal lpBuffer As System.Text.StringBuilder, ByRef i As Integer) As Boolean

User-Defined Types:

None.

Alternative Managed API:

Environment.UserName (System.Environment)

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

using System;
using System.Text;
using System.Runtime.InteropServices;
namespace GetUserNameExample
{
    class Class1
    {
        [DllImport("Advapi32.dll")]
        static extern bool GetUserName(StringBuilder lpBuffer, ref int nSize);    
        [STAThread]
        static void Main(string[] args)
        {
            StringBuilder Buffer = new StringBuilder(64);
            int nSize=64;
            GetUserName(Buffer, ref nSize);
            Console.WriteLine(Buffer.ToString());
        }
    }
}

Documentation
GetUserName on MSDN