To get the computer name from an IP address in C#, you can use the following approach:
csharpusing System;
using System.Net;
class Program
{
static void Main()
{
string ip = "192.168.1.1"; // Example IP address
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(ip);
string machineName = hostEntry.HostName;
Console.WriteLine($"Computer Name for IP {ip}: {machineName}");
}
catch (Exception ex)
{
Console.WriteLine($"Error getting computer name: {ex.Message}");
}
}
}
This code uses Dns.GetHostEntry
to retrieve the IPHostEntry
object containing information about the specified IP address. The HostName
property of IPHostEntry
gives you the computer name associated with that IP address.
Make sure to replace "192.168.1.1"
with the actual IP address you want to look up.
As for getting the IP address from the host name, you can use a similar approach:
csharpusing System;
using System.Net;
class Program
{
static void Main()
{
string hostname = "HITMAN-DESKTOP"; // Example hostname
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(hostname);
foreach (IPAddress ip in hostEntry.AddressList)
{
Console.WriteLine($"IP Address for Hostname {hostname}: {ip}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error getting IP address: {ex.Message}");
}
}
}
This code retrieves the IPHostEntry
object for the specified hostname and then iterates over the AddressList
property to get all IP addresses associated with that hostname.
Replace "HITMAN-DESKTOP"
with the actual hostname you want to look up. Note that the hostname resolution may fail if the hostname is not reachable or does not exist in your network.
المزيد من المعلومات
للحصول على المزيد من المعلومات حول اسم الكمبيوتر من عنوان IP في C#، يمكنك استخدام الكود التالي:
csharpusing System;
using System.Net;
class Program
{
static void Main()
{
string ip = "192.168.1.1"; // عنوان IP مثالي
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(ip);
string machineName = hostEntry.HostName;
Console.WriteLine($"اسم الكمبيوتر للعنوان {ip}: {machineName}");
}
catch (Exception ex)
{
Console.WriteLine($"حدث خطأ في الحصول على اسم الكمبيوتر: {ex.Message}");
}
}
}
يستخدم هذا الكود Dns.GetHostEntry
لاسترداد كائن IPHostEntry
الذي يحتوي على معلومات حول عنوان IP المحدد. يعطي خاصية HostName
لـ IPHostEntry
اسم الكمبيوتر المرتبط بهذا العنوان IP.
تأكد من استبدال "192.168.1.1"
بعنوان IP الفعلي الذي ترغب في البحث عنه.
أما بالنسبة للحصول على عنوان IP من اسم الاستضافة، فيمكنك استخدام نهج مماثل:
csharpusing System;
using System.Net;
class Program
{
static void Main()
{
string hostname = "HITMAN-DESKTOP"; // اسم الاستضافة مثالي
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(hostname);
foreach (IPAddress ip in hostEntry.AddressList)
{
Console.WriteLine($"عنوان IP للاستضافة {hostname}: {ip}");
}
}
catch (Exception ex)
{
Console.WriteLine($"حدث خطأ في الحصول على عنوان IP: {ex.Message}");
}
}
}
يقوم هذا الكود بجلب كائن IPHostEntry
للاستضافة المحددة ثم يكرر عبر الخاصية AddressList
للحصول على جميع عناوين IP المرتبطة بهذا الاسم المضيف.
استبدل "HITMAN-DESKTOP"
بالاسم المضيف الفعلي الذي تريد البحث عنه. يجب ملاحظة أن فشل قرار اسم المضيف قد يحدث إذا كان اسم المضيف غير قابل للوصول أو غير موجود في شبكتك.