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


//returns COMPUTERNAME
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(); 
        break;
    }
}

Console.WriteLine(ipAddress);
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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