Get Computer Name, Local IP Address and User Name in C#

string computerName = Environment.MachineName; 

//returns "computername\username"
string computerAndUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 

//returns username
string userName2 = Environment.UserName; 

//Returns A string that contains the DNS host name of the local computer.
string hostName = Dns.GetHostName();  

//IPHostEntry-  Internet host address information.
//Dns.GetHostEntry(String)- Resolves a host name or IP address to an IPHostEntry instance.
IPHostEntry host = Dns.GetHostEntry(hostName);

//Gets or sets a list of IP addresses that are associated with a host.
IPAddress[] ipAddresses = host.AddressList;

string ipAddress = String.Empty;
foreach (IPAddress ip in ipAddresses)
    if (ip.AddressFamily.ToString()=="InterNetwork")
        ipAddress = ip.ToString(); 


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s