Click here to Skip to main content
15,914,405 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ,

I am trying to get client computer name using C# many way but not get pc name code is below:

C#
public ActionResult GetCleintPCName(string sid)
        {
            string PC1 = System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName.ToString();
            string PC2 = System.Environment.MachineName;
            string pcname = PC1 + " ,1: " + PC2;

            string computer_name = System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_HOST"]).HostName.ToString();

            string IP = Request.UserHostName;
            IPAddress myIP = IPAddress.Parse(IP);
            IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
            List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
            string comname= compName.First();
            pcname = pcname + " 2:" + computer_name + " 3:" + comname;

            return Json(new { pcName = pcname }, JsonRequestBehavior.AllowGet);
        }

It working no localhost but not on server.

It returns server name.I also tried java script and Jquery but not get expected result.

I am working on internet web application.I want to get pc name and based on pc i get attached printer like label printer and hp printer and then give print command. For printing i am using neodynomic web client dll.

How can i Get MAC address of client machine in different network?

Please give me any link or suggestion.
Posted
Updated 7-May-14 23:50pm
v4
Comments
ZurdoDev 7-May-14 7:41am    
Are all your computers on the same network? Because otherwise it won't make any sense.
Thanks7872 7-May-14 7:45am    
Suppose you got Admin-PC for some network then what? How its useful? I mean, why you want Computername?

Use this to get client Machine Name

C#
HttpRequest request = base.Request;

        // Get UserHostAddress property.
        string address = Request.ServerVariables["REMOTE_USER"];
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 7-May-14 8:27am    
That will give you a name only in special cases! In most cases it will be an IP or empty...
There is no way to get name for sure - it depends on the ability of the various DNS servers between your server and the client.
HttpRequest.UserHostName[^] should give you that name, but in most cases it gives IP address...
 
Share this answer
 
Comments
Sanket Saxena 7-May-14 8:24am    
Correct because in order to retrieve the client’s computer name you will have to query the DNS server with the client’s IP
Kornfeld Eliyahu Peter 7-May-14 8:27am    
And who told that client's IP is on any DNS at all...
Sanket Saxena 7-May-14 8:29am    
no way thats what i am saying.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900