Wednesday, June 24, 2009

Environment variables in C#

C# syntax:
To get the logged-in username:
string username = System.Environment.GetEnvironmentVariable("USERNAME");

To get the name of the client machine:
string machinename = Environment.MachineName;

1 comment:

Ruchi said...

This doesnt work when anonymous login is not enabled on IIS. In such cases, use the below for getting the logged in username:

string username = Page.User.Identity.Name;

About Us