Solved

How to get my pc's ip?

Posted on 2006-07-13
9
272 Views
Last Modified: 2008-02-01
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
Comment
Question by:turbot_yu
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 8

Assisted Solution

by:Shakti109
Shakti109 earned 50 total points
ID: 17105366

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
 
LVL 5

Assisted Solution

by:nickhoggard
nickhoggard earned 200 total points
ID: 17105512
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
 
LVL 5

Assisted Solution

by:nickhoggard
nickhoggard earned 200 total points
ID: 17105526
0
 
LVL 9

Accepted Solution

by:
jrscherer earned 100 total points
ID: 17105828
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:turbot_yu
ID: 17106009
The OS is xp. The version is 2003.

What is the full name of NIC? Thanks.
0
 
LVL 11

Assisted Solution

by:LordWabbit
LordWabbit earned 150 total points
ID: 17106462
Network Interface Card
0
 
LVL 11

Assisted Solution

by:LordWabbit
LordWabbit earned 150 total points
ID: 17106473
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
 

Author Comment

by:turbot_yu
ID: 17117364
There is no bluetooth, what is vmware, thanks.
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 17120028
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now