Click here to Skip to main content
13,199,318 members (44,844 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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
Posted 21-Jul-15 20:17pm
Comments
Richard MacCutchan 22-Jul-15 3:06am
   
What exactly are you trying to do? If you can find the IP address of any machine on the lan then you have all the information you need to connect to them.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171020.1 | Last Updated 22 Jul 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100