Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

How to get my pc's ip?

I am using ADSL connection, the ip may change, my program need to check for fixed time interval of the pc's ip.
Is there any sample code?

Then I need to write it to a log file and write to DB.

Thx
0
turbot_yu
Asked:
turbot_yu
  • 3
  • 2
  • 2
  • +2
6 Solutions
 
Shakti109Commented:

Which version of O/S will the application run on?

If it is pre-XP, then the function to determine I.P will be a little more involved.
If it is XP and above a VERY simpe call to a native method will do it.

Also, which version of studio will you be developing in? 2003 or 2005?

Answer these two questions and we can find the right path for you to take.
0
 
nickhoggardCommented:
Hi,

Check out the System.Net.DNS class.  The following article covers getting the IP address from it:
http://www.codeproject.com/csharp/network.asp

Cheers

Nick
0
 
nickhoggardCommented:
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jrschererCEOCommented:
Hi,
Your question is a little bit tricky, since you may have several NICs installed and active in your computer. Each NIC may have several IP addresses allocated to it.

Here is a function to get all IP addresses in your system of all enabled NICs. May be this can help you to find your solution
You will need to have a Reference to System.Management and import it.

    Private Function GetAllActiveIPs() As String
        Dim al As New ArrayList
        Try
            Dim mc As ManagementClass
            Dim mo As ManagementObject
            mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
            Dim moc As ManagementObjectCollection = mc.GetInstances()

            For Each mo In moc
                If CBool(mo.Item("IPEnabled")) Then

                    Dim str() As String = CType(mo.Item("IPAddress"), String())

                    For Each strx As String In str
                        al.Add(strx)
                    Next

                End If
            Next
        Catch ex As Exception
            Return ex.ToString
        End Try

        Dim strg As String = String.Empty
        For Each strgx As String In al
            strg &= strgx & vbCrLf
        Next

        Return strg
    End Function

Place a button and a label onto your form and

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = GetAllActiveIPs()
    End Sub

Jack.net

0
 
turbot_yuAuthor Commented:
The OS is xp. The version is 2003.

What is the full name of NIC? Thanks.
0
 
LordWabbitCommented:
Network Interface Card
0
 
LordWabbitCommented:
Oh yeah forgot to mention if you are running a bluetooth device it will also have a IP address, also if you have vmware installed it adds two virtual nics as well so it's not always straight forward to programatically determine the PC's ip address (not sure what microsofts vitual pc does, installed it once didn't like it much)
0
 
turbot_yuAuthor Commented:
There is no bluetooth, what is vmware, thanks.
0
 
LordWabbitCommented:
vmware is a virtual pc emulator, chances are if you don't know what it is, it's not installed either so no worries.  Using vmware you can create another virtual pc which runs on your current os, whats the point you ask, well if you are deploying into a win2k3 environment you can set up one in a virtual pc to test the deployment process / docs etc.  Also we had a case where some of a clients customers where still running win98 (sad but true) and our software had to work on that, so we set up a virtual pc running windows 98 to test if we were using functions / methods which would not work in that OS.
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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now