I plead not guilty, editing this in an attempt to slightly improve it has put my name under it all. I did not write this, all I did was add some comment lines. Luc Pattyn, 28-DEC-2009.
This simple code displays how to get machine IP address, Current user name and logged in domain name
. Also this code writes all above details into a text file in the local machine.
You need to call following libraries in .NET framework.
using System.IO;using System.Net;using System.Diagnostics;
string pcname, domain, username, ip;
string filepath="C:\\PCdata"; string logname = "PCInfo.txt";
pcname = System.Environment.MachineName; domain = System.Environment.UserDomainName; username = System.Environment.UserName;
This code will read the IP address of your machine.
protected string Get_IP_Address()
IPHostEntry ipentry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress iparray = ipentry.AddressList;
for (int i = 0; i < iparray.Length; i++)
ip = iparray[i].ToString(); return ip;
And finally, use this code to write your log file (textfile) inside a button click event or any other place.
private void Write_LogOff_File()
FileStream fs = new FileStream(@filepath + logname,FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.WriteLine(pcname + " | " + domain + " | " + username + " | " + ip);
Remember you can put all these code in any place of your program. Such as button click event, form load or even in form closing.