C# Application Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use the Environment.UserName
property to get the current username in .NET. Here is an example:
string userName = Environment.UserName; Console.WriteLine("Current username: " + userName);
This will output the current username.
You can use the User.Identity.Name
property to get the current user in ASP.NET MVC. Here is an example:
string userName = User.Identity.Name; ViewBag.UserName = userName;
This will set the ViewBag.UserName
property to the current user's name.
You can use the UserPrincipal
class from the System.DirectoryServices.AccountManagement
namespace to get the email address of the current user in a Windows domain. Here is an example:
using System.DirectoryServices.AccountManagement; PrincipalContext principalContext = new PrincipalContext(ContextType.Domain); UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, Environment.UserName); string emailAddress = userPrincipal.EmailAddress; Console.WriteLine("Current user's email address: " + emailAddress);
This will output the email address of the current user in the Windows domain.
You can use the User.Identity.Name
property to get the current user in ASP.NET Core. Here is an example:
string userName = User.Identity.Name; ViewBag.UserName = userName;
This will set the ViewBag.UserName
property to the current user's name.
You can use the WindowsIdentity
class to get the SID of the current Windows account. Here is an example:
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent(); string userSid = windowsIdentity.User.Value; Console.WriteLine("Current user SID: " + userSid);
This will output the SID of the current Windows account.
Get Current User in C# Application:
Use Environment.UserName
to retrieve the current user's name.
using System; class Program { static void Main() { string currentUser = Environment.UserName; Console.WriteLine($"Current User: {currentUser}"); } }
C# Get Current Windows Username:
Retrieve the current Windows username using Environment.UserName
.
using System; class Program { static void Main() { string windowsUsername = Environment.UserName; Console.WriteLine($"Windows Username: {windowsUsername}"); } }
Environment.UserName in C#:
Access the current user's name using Environment.UserName
.
using System; class Program { static void Main() { string currentUser = Environment.UserName; Console.WriteLine($"Current User: {currentUser}"); } }
WindowsIdentity.GetCurrent C# Example:
Use WindowsIdentity.GetCurrent
to retrieve information about the current Windows user.
using System; using System.Security.Principal; class Program { static void Main() { WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(); Console.WriteLine($"Current User: {currentIdentity.Name}"); } }
C# Get Current User Domain and Username:
Use Environment.UserDomainName
and Environment.UserName
to get the current user's domain and username.
using System; class Program { static void Main() { string userDomain = Environment.UserDomainName; string userName = Environment.UserName; Console.WriteLine($"User: {userDomain}\\{userName}"); } }
WindowsPrincipal.GetCurrent in C#:
Use WindowsPrincipal.GetCurrent
to access the current Windows user's principal.
using System; using System.Security.Principal; class Program { static void Main() { WindowsPrincipal currentPrincipal = WindowsPrincipal.GetCurrent(); Console.WriteLine($"Current User: {currentPrincipal.Identity.Name}"); } }
GetCurrentUser Function in C#:
Implement a custom function to get the current user.
using System; class Program { static void Main() { string currentUser = GetCurrentUser(); Console.WriteLine($"Current User: {currentUser}"); } static string GetCurrentUser() { return Environment.UserName; } }
C# Get Current User SID:
Use WindowsIdentity.GetCurrent().User.Value
to get the current user's SID.
using System; using System.Security.Principal; class Program { static void Main() { SecurityIdentifier currentUserSID = WindowsIdentity.GetCurrent().User; Console.WriteLine($"Current User SID: {currentUserSID.Value}"); } }
Getting the Current User in WPF C#:
Retrieve the current user in a WPF application.
using System; using System.Windows; class MainWindow : Window { public MainWindow() { string currentUser = Environment.UserName; MessageBox.Show($"Current User: {currentUser}"); } }
GetCurrentUser() Method in C# Windows Forms:
Get the current user in a Windows Forms application.
using System; using System.Windows.Forms; class MainForm : Form { public MainForm() { string currentUser = GetCurrentUser(); MessageBox.Show($"Current User: {currentUser}"); } string GetCurrentUser() { return Environment.UserName; } }