A Function to Get IP Address of a Domain

Code example will do please, thanks no algorithms.

I need a C# or VB.NET code function to return the IP address of a domain.

For example if I have the domain named as www.mydomain.com or mydomain.co.uk
I will need the IP address returned. I will need to do this in a batch so please
keep that in mind when coding, possibly threaded, thanks.
netimpactAsked:
Who is Participating?
 
CartoonHeadConnect With a Mentor Commented:
This works fine in a WinForm... Framework 2.0
Pretty much the same as MuhammadAdil's code.
private void button1_Click(object sender, EventArgs e)
        {
            IPHostEntry ip = Dns.GetHostEntry("www.yahoo.com");
            this.Text = ip.AddressList[0].ToString();
        }

Open in new window

0
 
MuhammadAdilConnect With a Mentor Commented:
public string GetDomainIP(string domain)
        {
            IPHostEntry objIPHostEntry;
            objIPHostEntry = Dns.Resolve(domain); // relax-solutions.com
            return objIPHostEntry.AddressList[0].ToString();
        }

Regards,
Adil Fazal
0
 
netimpactAuthor Commented:
It failed on ==> objIPHostEntry = Dns.Resolve(domain);

The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for

System.Net.Sockets.SocketException was unhandled
  ErrorCode=11004
  Message="The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for"
  Source="System"
  StackTrace:
       at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
       at System.Net.Dns.Resolve(String hostName)
       at ParseTextFileNS.MainForm.GetDomainIP(String domain) in F:\...\MainForm.vb:line 137
       at ParseTextFileNS.MainForm.Button1_Click(Object sender, EventArgs e) in F:\...\MainForm.vb:line 148
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at ParseTextFileNS.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Stephen MandersonConnect With a Mentor Software EngineerCommented:
This is the function I use for this in a vb app as cartoons and muhammeds pretty much the same. Tested working in vs2005 & vs2008


Imports System.Net
.....
.........
    Public Function GetIPofURL(ByVal URL As String) As String
        Dim GetIP As IPHostEntry = Dns.GetHostEntry(URL)
        Return GetIP.AddressList(0).ToString
    End Function

Open in new window

0
 
MuhammadAdilCommented:
Dont use http and www with domain name. Etc. Google.com, yahoo.com
0
 
Stephen MandersonSoftware EngineerCommented:
You can use the full address with www. with gethostentry also .resolve has been replaced by gethostentry
0
 
CartoonHeadCommented:
It works with localhost etc... So I guess in a local domain 'PCName' would also return OK.
Or have I got your question wrong?
0
 
netimpactAuthor Commented:
CartoonHead:
Thank you ,your code worked for me.

Quick question if you don't mind. Would like my WinApps to look like Office 2007 lookalike how do I do that? Any articles will be good.

I have seen a few new apps like that and I want to find out how get that look and feel, thanks.
0
 
CartoonHeadCommented:
I think that is for another question - Head down the WPF path though
0
 
netimpactAuthor Commented:
Thanks I have found what I am looking for it is called DotNetBar.
0
 
Stephen MandersonSoftware EngineerCommented:
Look at devexpress
0
 
netimpactAuthor Commented:
MrManderson:

Your code also worked.
0
 
netimpactAuthor Commented:
MrManderson:
Do you do dev on the side?
0
 
Stephen MandersonSoftware EngineerCommented:
I do a bit of everything :-)

Server, security, programming, databases etc.. Headache :P
0
 
netimpactAuthor Commented:
CartoonHead:
Do you do development on the side?
0
 
netimpactAuthor Commented:
MrManderson:

You have a website?
0
 
Stephen MandersonSoftware EngineerCommented:
Sort of :-) not finished it yet address details on my profile
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.