I have this issue which I did not find an answer to it.

I am creating a small app which SUPPOSE to be easy and it is about getting 4 things (User Name, Pc Name, Domain Name, and IP Address)

I have over 35 pc's connected to a domain, and diff user may logged in to a single pc.

I want the ability that my exe could run from network path \\server\folder\myexe.exe instead of copy the file to each pc local HDD.

I create the program and build it, and put the exe on the UNC, I dblclick on it from my pc (Vista), it works fine.

I tried to run the exe from another pc (XP SP2) it gives security warning "Unhandled exception has occurred in your application, if you click continue, the application will ignore this error and attemp to continue. If you click quit, the application will close immediately
Index was outside the bounds of the array.

here is original code

Dim wsNet As Object
wsNet = CreateObject("WScript.Network")
txt_UserName.Text = wsNet.UserName
txt_DomainName.Text = wsNet.UserDomain
txt_PcName.Text = wsNet.ComputerName
Dim _HostName = System.Net.Dns.GetHostName()
Dim _IPAdress = System.Net.Dns.GetHostAddresses(_HostName)(0).ToString
txt_IP.Text = _IPAdress

- The above code works fine on my pc (Vista) if I put it on local HDD or UNC
- The above code also works fine on every pc (XP SP2) if copied to local HDD but will give the mentioned error if run from UNC

- I had comment the first 5 lines, and test it from UNC and the exe works fine on every pc. so, I thought that CreateObject("WScript.Network") is giving this, here is my tests.

My.User.CurrentPrincipal.Identity.Name.ToString ' <-- Gives Error
My.User.Name ' <-- Works from develpment pc and not from other work station 
Environment.UserDomainName ' <-- Gives Error

I even tried GetUserName() and GetUserNameEx() API and they both gives Error

here is my final code which is working fine except for the domain name.

txt_UserName.Text = Environment.UserName
Dim _HostName = System.Net.Dns.GetHostName()
txt_PcName.Text = _HostName
Dim _IPAdress = System.Net.Dns.GetHostAddresses(txt_PcName.Text)(0).ToString
txt_IP.Text = _IPAdress

what else I can use to get the Domain Name?

The problem is the line of code that reads:

Dim _IPAdress = System.Net.Dns.GetHostAddresses(_HostName)(0).ToString

The code is more complex than what you are using. Try using this class to get at the IPAddress information:

Imports System.Net
Imports System.Net.Sockets

Friend Class IPAddressClass

    Private pvtIPv4AddressString As String
    Private pvtIPv6AddressString As String
    Private pvtSupportsIPv4Boolean As Boolean
    Private pvtSupportsIPv6Boolean As Boolean
    Private pvtAddressFamilyString As String
    Private pvtScopeIDString As String
    Private pvtMachineNameString As String

    ReadOnly Property IPv4Address() As String
            IPv4Address = pvtIPv4AddressString
        End Get
    End Property

    ReadOnly Property IPv6Address() As String
            IPv6Address = pvtIPv6AddressString
        End Get
    End Property

    ReadOnly Property SupportsIPv4() As Boolean
            SupportsIPv4 = pvtSupportsIPv4Boolean
        End Get
    End Property

    ReadOnly Property SupportsIPv6() As Boolean
            SupportsIPv6 = pvtSupportsIPv6Boolean
        End Get
    End Property

    ReadOnly Property AddressFamily() As String
            AddressFamily = pvtAddressFamilyString
        End Get
    End Property

    ReadOnly Property ScopeID() As String
            ScopeID = pvtScopeIDString
        End Get
    End Property

    ReadOnly Property MachineName() As String
            MachineName = pvtMachineNameString
        End Get
    End Property

    Private Sub IPAddresses(ByVal server As String)
            ' Get server related information.
            Dim heserver As IPHostEntry = Dns.GetHostEntry(server)

            ' Loop on the AddressList
            Dim curAdd As IPAddress

            pvtMachineNameString = server

            For Each curAdd In heserver.AddressList

                ' Display the type of address family supported by the server. If the
                ' server is IPv6-enabled this value is: InternNetworkV6. If the server
                ' is also IPv4-enabled there will be an additional value of InterNetwork.
                pvtAddressFamilyString = curAdd.AddressFamily.ToString()

                ' Display the ScopeId property in case of IPV6 addresses.
                If curAdd.AddressFamily.ToString() = ProtocolFamily.InterNetworkV6.ToString() Then
                    pvtSupportsIPv6Boolean = True
                    pvtScopeIDString = curAdd.ScopeId.ToString()
                    ' Display the server IP address in the standard format. In 
                    ' IPv4 the format will be dotted-quad notation, in IPv6 it will be
                    ' in in colon-hexadecimal notation.
                    pvtIPv6AddressString = curAdd.ToString()
                    pvtSupportsIPv4Boolean = True
                    pvtIPv4AddressString = curAdd.ToString()
                End If

            Next curAdd

        Catch e As Exception
            ' Console.WriteLine(("[DoResolve] Exception: " + e.ToString()))
        End Try
    End Sub

    Friend Sub GetInfo()
        Dim server As String

        server = Dns.GetHostName()

    End Sub

End Class

Thank you for this class which I guess it will be very helpful in version 3 of my software :)

The problem in short is, some vb command works fine if the exe run from unc and some gives security warning.

The Line commented by you about the ip address is working fine since all the pc in the company work with ip4

I will give you an example

Environment.UserDomainName ' <-- Gives Error
Environment.UserName ' <-- works fine

The above both works fine if the exe exist on local HDD, if the exe exist in UNC "\\server\folder" the first will give an error and the second will work fine.

Why is that?

I had managed to get all the info I need except domain name.


Try using this line of code to get the domain name:

Dim DomainName as string = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties.DomainName.ToString()

Also, add the class to your project and call the GetInfo() method of the class. Then look at the public properties to get the IP address(es).

Hi rdefazio,

Dim DomainName as string = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties.DomainName.ToString()

I had tried your code, and it gives this result.

- It don't run on Vista local HDD neither UNC
- It works on XP SP2 Local HDD not UNC

I had given up searching to fix this, I am officially declare I had surrender :)

I will copy each exe to each pc local hdd and end this.

Thank you for tour time and effort.