Share via


C# 3.0 - Get LoggedIn UserName, ComputerName and IP Address

Question

Thursday, March 13, 2008 6:16 AM

Hi,

 

What should be C# 3.0 code (and references) to retrieve:

Logged in user name

Computer name

and IP Address?

 

Thanks in advance,

Aldo.

All replies (10)

Thursday, March 13, 2008 6:32 AM ✅Answered

For UserName = Try System.Security.Principal.WindowsIdentity.GetCurrent().User

For ComputerName= Try System.Security.Principal.WindowsIdentity.GetCurrent()Name

 

IP address there are many ways.

Question. What IP you need the router IP or the Machine IP?

 


Thursday, March 13, 2008 8:18 AM ✅Answered | 3 votes

You could use

 

Environment.UserName to get current logged in user name. Note that Environment is from System namespace itself

Environment.MachineName to get the local machine name

 

You can also use Dns.GetHostName() to get local machine name. Note that Dns is from System.Net namespace.

 

Finally

Dns.GetHostEntry(Dns.GetHostName()) will return IPHostEntry.

 

You can loop thro' the IPHostEntry on the AddressList to get the assigned IP addresses to the machine

 


Thursday, March 13, 2008 1:02 PM ✅Answered | 1 vote

What's the difference between Environment and System.Security?

I am using it as follows. What I don't understand is why, when running I receive only 1 IP address.

Thanks again,

Aldo.

 

Code Snippet

namespace Test

{

class Test

{

static void Main(string[] args)

{

Console.WriteLine("Current Logged UserName: " + Environment.UserName);

Console.WriteLine("Local Machine Name: " + Environment.MachineName);

Console.WriteLine("Local Machine Name: " + Dns.GetHostName()); // Dns from System.Net

// Console.WriteLine("IPHostEntry: " + Dns.GetHostByName(Dns.GetHostName)); // Dns from System.Net

IPHostEntry ip = Dns.GetHostEntry (Dns.GetHostName());

IPAddress[] IPaddr = ip.AddressList;

for (int i = 0; i < IPaddr.Length; i++)

{Console.WriteLine("IP Address {0}: {1} ", i, IPaddr[i].ToString());}

Console.ReadLine();

}

}

}

 

 


Friday, March 14, 2008 12:57 PM ✅Answered

Using the Environment and System.Security class requires different permissions. You can check up the SDK for more details.

 

Additionally, you need to worry about where you are calling this from. In a WinApplication, the application is run under the ID of the person logged onto the machine.

 

However in Web scenario you may be more interested in knowing either the process thread identity (under which the asp.net application is running) or the logged in user (who is currently accessing the application). In either case, Environment.UserName will not help

 


Tuesday, July 1, 2008 11:34 AM

Hi!
Thanks for the code for local IP address.
How can I find out my local port?

In C++ with winsock2.lib:

    // Reset the SOCKADDR_IN struct
memset(&sckAddress, 0, sizeof(sockaddr_in));
     // Setup the sckClient socket
sckClient = socket(AF_INET, SOCK_STREAM, 0);
sckAddress.sin_port = htons(atoi(port));
sckAddress.sin_family = AF_INET;
sckAddress.sin_addr.s_addr = inet_addr(host);

I tried the following:

                IPAddress ipAddr = GetLocalIP();
                IPEndPoint LocalIpEndPoint = new IPEndPoint(ipAddr, 0);

                UInt16 localPort = (UInt16)LocalIpEndPoint.Port;

But the local port is always 0.  I need to supply an actual port number to the UDP Handshake, so that the unit on the receiving side will know how to contact my application.

Thanks!
Mechi


Tuesday, July 1, 2008 3:09 PM

There is no such thing as a local port? What are you looking for? There are 64,000 TCP/IP ports and 64,000 UDP ports. They are all up for grabs by any socket that needs them (although some of them are dedicated, like 25 for email and 110 for http and a number of others--for internal OS needs). 


AlexB


Wednesday, July 2, 2008 6:28 AM

Hi!
I think i figured it out.
I have to send the remote machine the port that our peer-to-peer communication will be using.  There will be much data streaming and my application and the remote device have to use the same port.

udpClient.Connect(IPAddress.Parse(mmIPaddr), remotePort);
IPEndPoint localEndPoint = (IPEndPoint)udpClient.Client.LocalEndPoint;
UInt16 localPort = (UInt16)localEndPoint.Port;

After the Connect, the UDPclient has a value in the Port.
Mechi


Mechi


Tuesday, February 10, 2009 2:29 PM

How do I get Machine IP address / name for a given logged on windows user name?

Also is it possible to determine if a given username is currently logged on or logged off?
Bhavesh


Thursday, September 24, 2009 4:59 AM

Dear Bermil

     Is it possible to get the IP address of the router using C# code..
Please help me out

Regards

Arun


Saturday, October 16, 2010 10:26 AM

Hi,

 I want to find system which are connected in network their ipaddress as  well as their name using asp.net application . how to get it .

 

Regards

 

Nitin