在c#中手动设置连接到pc热点的设备的IP地址 [英] Set a IP Address of devices connected to pc hotspot manually in c#

查看:224
本文介绍了在c#中手动设置连接到pc热点的设备的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何在c#


$ b中手动设置连接到PC热点的设备的IP地址$ b我试图在谷歌找到它但发现这一个



它会自动获取IP





这里是代码



 使用系统; 
使用 System.Collections.Generic;
使用 System.Diagnostics;
使用 System.Linq;
使用 System.Net;

命名空间 Hotspot.Api
{
/// < summary >
/// 此类允许您检索当您只知道其MAC地址时,本地网络上特定计算机的IP地址和主机名。
/// < / summary >
public class IPInfo
{
public IPInfo( string macAddress, string ipAddress)
{
this .MacAddress = macAddress;
this .IPAddress = ipAddress;
}

public string MacAddress {获得; private set ; }
public string IPAddress { get ; private set ; }
private string hostName = null ;
public string HostName
{
get
{
if (hostName == null
{
if this .IPAddress!= null
尝试
{
hostName = Dns.GetHostEntry( .IPAddress).HostName;
}
catch (例外)
{
}
}
return hostName;
}
}

#region静态方法

/// < 摘要 >
/// < span class =code-comment>使用指定的MAC地址检索本地网络上的计算机的IPInfo。
/// < / summary >
/// < param < span class =code-summarycomment> name =macAddress > MAC要检索的IPInfo的地址。< / param > ;
/// < 返回 > < < /跨度> / returns >
public static IPInfo GetIPInfo( string macAddress)
{
var ipinfo =( from ip in IPInfo.GetIPInfo ()
其中 ip.MacAddress.ToLowerInvariant()== macAddress.ToLowerInvariant()
选择 ip).FirstOrDefault();

return ipinfo;
}

/// < 摘要 >
/// 检索本地网络上所有计算机的IPInfo。
/// < / summary >
/// < 返回 < span class =code-summarycomment>> < / returns >
public static 列表< IPInfo> GetIPInfo()
{
尝试
{
var list = new List< IPInfo>();

var splitted = textbox1.text()。分割( new char [] {' \ n'' \ r'});
foreach var arp in splitted)
{
// 解析所有MAC / IP地址组合
if (!string.IsNullOrEmpty(arp))
{
var pieces =( from piece in arp.Split( new char [] {' ' ' \t'})
where !string.IsNullOrEmpty(piece)
select piece).ToArray();
if (pieces.Length == 3
{
list.Add( new IPInfo(件[ 1 ],件[ 0 ]));
}
}
}

// 返回包含MAC / IP地址组合的IPInfo对象列表
return 列表;
}
catch (例外情况)
{
throw new 异常( IPInfo:Error Parsing'arp -a'结果',ex);
}
}

/// < 摘要 >
/// 这将在Windows中运行arp实用程序以检索所有MAC / IP地址条目。
/// < / summary >
/// < 返回 < span class =code-summarycomment>> < / returns >
private static string GetARPResult()
{
流程p = null ;
string output = string .Empty;

尝试
{
p = Process.Start( new ProcessStartInfo( arp -
{
CreateNoWindow = true
UseShellExecute = false
RedirectStandardOutput = true
});

output = p.StandardOutput.ReadToEnd();

p.Close();
}
catch (例外情况)
{
throw new 异常( IPInfo:错误检索'arp -a'结果',ex);
}
最后
{
如果(p != null
{
p.Close();
}
}

return 输出;
}

#endregion
}
}









所以这是代码

它会自动解析主机名ip地址也是。

但是我需要它自动解析主机名,但手动输入ip地址为ex =



Textbox1.text = 192.168。 21.4





请帮助

解决方案

为了设置IP要使用系统中任何设备的地址,您需要成为网段的DHCP主机 - 其中包括示例中192.168.21.xxx区域的所有设备。如果不是,那么两个设备很容易被赋予相同的IP地址,这将导致很多问题。而且这个角色通常是给路由器或类似的设备,而不是特定的PC(虽然它可以)。

所以首先看看你的本地网络,并确定DHCP服务器 - 你会必须与它沟通才能做到这一点。



我希望你有充分的理由这样做,而且听起来好像没有很多奖励!

Hey

How can i set a ip address of devices connected to pc hotspot manually in c#

I tried to find it on google but found this one

It gets ip but automatically


here is the code

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;

namespace Hotspot.Api
{
	/// <summary>
	/// This class allows you to retrieve the IP Address and Host Name for a specific machine on the local network when you only know it's MAC Address.
	/// </summary>
	public class IPInfo
	{
		public IPInfo(string macAddress, string ipAddress)
		{
			this.MacAddress = macAddress;
			this.IPAddress = ipAddress;
		}

		public string MacAddress { get; private set; }
		public string IPAddress { get; private set; }
		private string hostName = null;
		public string HostName
		{
			get
			{
				if (hostName == null)
				{
					if (this.IPAddress != null)
						try
						{
							hostName = Dns.GetHostEntry(this.IPAddress).HostName;
						}
						catch (Exception)
						{
						}
				}
				return hostName;
			}
		}

		#region "Static Methods"

		/// <summary>
		/// Retrieves the IPInfo for the machine on the local network with the specified MAC Address.
		/// </summary>
		/// <param name="macAddress">The MAC Address of the IPInfo to retrieve.</param>
		/// <returns></returns>
		public static IPInfo GetIPInfo(string macAddress)
		{
			var ipinfo = (from ip in IPInfo.GetIPInfo()
						  where ip.MacAddress.ToLowerInvariant() == macAddress.ToLowerInvariant()
						  select ip).FirstOrDefault();

			return ipinfo;
		}

		/// <summary>
		/// Retrieves the IPInfo for All machines on the local network.
		/// </summary>
		/// <returns></returns>
		public static List<IPInfo> GetIPInfo()
		{
			try
			{
				var list = new List<IPInfo>();

				var splitted = textbox1.text().Split(new char[] { '\n', '\r' });
				foreach (var arp in splitted)
				{
					// Parse out all the MAC / IP Address combinations
					if (!string.IsNullOrEmpty(arp))
					{
						var pieces = (from piece in arp.Split(new char[] { ' ', '\t' })
									  where !string.IsNullOrEmpty(piece)
									  select piece).ToArray();
						if (pieces.Length == 3)
						{
							list.Add(new IPInfo(pieces[1], pieces[0]));
						}
					}
				}

				// Return list of IPInfo objects containing MAC / IP Address combinations
				return list;
			}
			catch (Exception ex)
			{
				throw new Exception("IPInfo: Error Parsing 'arp -a' results", ex);
			}
		}

		/// <summary>
		/// This runs the "arp" utility in Windows to retrieve all the MAC / IP Address entries.
		/// </summary>
		/// <returns></returns>
		private static string GetARPResult()
		{
			Process p = null;
			string output = string.Empty;

			try
			{
				p = Process.Start(new ProcessStartInfo("arp", "-a")
				{
					CreateNoWindow = true,
					UseShellExecute = false,
					RedirectStandardOutput = true
				});

				output = p.StandardOutput.ReadToEnd();

				p.Close();
			}
			catch (Exception ex)
			{
				throw new Exception("IPInfo: Error Retrieving 'arp -a' Results", ex);
			}
			finally
			{
				if (p != null)
				{
					p.Close();
				}
			}

			return output;
		}

		#endregion
	}
}





So it is the code
it resolves hostname automatically and ip address too.
But i need it resolves hostname automatically but ip address manually given in for ex=

Textbox1.text= 192.168.21.4


pls help

解决方案

In order to set the IP address of any device in your system, you need to be the DHCP host for the network segment - which includes all devices on the 192.168.21.xxx area in your example. If it wasn't, then two devices could easily be given the same IP address and that would cause a lot of problems. And that role is normally given to a router or similar equipment, rather than a specific PC (although it can be).
So start by looking at your local network, and identify the DHCP server - you will have to communicate with it to do this.

I hope you have a good reason for this, and it sounds like a lot of work for not a lot of reward!


这篇关于在c#中手动设置连接到pc热点的设备的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆