PC Review


Reply
Thread Tools Rate Thread

Convert from network name to ip address

 
 
Roidy
Guest
Posts: n/a
 
      31st May 2010
I have a function that reads a file but fails if I pass it a network device
name instead of an ip address eg:-

ReadFunction("\\NAS\Myfolder\Myfile.txt") <--- Fails
ReadFunction("\\192.168.2.2\Myfolder\Myfile.txt") <--- Works

So how do I convert a network devices name to it's ip address?
ie. Convert \\NAS\ to \\192.168.2.2\

Thanks
Robert




 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      31st May 2010
Am 31.05.2010 14:56, schrieb Roidy:
> I have a function that reads a file but fails if I pass it a network device
> name instead of an ip address eg:-
>
> ReadFunction("\\NAS\Myfolder\Myfile.txt") <--- Fails
> ReadFunction("\\192.168.2.2\Myfolder\Myfile.txt") <--- Works
>
> So how do I convert a network devices name to it's ip address?
> ie. Convert \\NAS\ to \\192.168.2.2\



Try this: (passing "NAS")

System.Net.Dns.GetHostByName(ByVal String) As System.Net.IPHostEntry

Does it fail, too?


--
Armin
 
Reply With Quote
 
Roidy
Guest
Posts: n/a
 
      31st May 2010
Thanks Armin,

VB complains that GetHostByName is obsolete and tells you to use
GetHostEntry, which works perfectly:-

Dim hostname As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("NAS")
Dim ipaddress() As System.Net.IPAddress = hostname.AddressList

Now it just an easy task to write a function to strip out the network device
name from the path and replace it with the resulting IP address.

Thanks
Robert


"Armin Zingler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Am 31.05.2010 14:56, schrieb Roidy:
>> I have a function that reads a file but fails if I pass it a network
>> device
>> name instead of an ip address eg:-
>>
>> ReadFunction("\\NAS\Myfolder\Myfile.txt") <--- Fails
>> ReadFunction("\\192.168.2.2\Myfolder\Myfile.txt") <--- Works
>>
>> So how do I convert a network devices name to it's ip address?
>> ie. Convert \\NAS\ to \\192.168.2.2\

>
>
> Try this: (passing "NAS")
>
> System.Net.Dns.GetHostByName(ByVal String) As System.Net.IPHostEntry
>
> Does it fail, too?
>
>
> --
> Armin
>

 
Reply With Quote
 
Roidy
Guest
Posts: n/a
 
      31st May 2010
This is what I knocked together to check the path to see if it's network
path and then replace the device name with it's IP address. Seems to work,
can you see any problems I might face using this? or is there a better way?

Public Function PathFixer(ByVal path As String) As String
If path.StartsWith("\\") Then ' Check to see if it's a network path
Dim output As String
Dim splits() As String = path.Substring(2, path.Length -
2).Split("\")
Dim hostname As System.Net.IPHostEntry =
System.Net.Dns.GetHostEntry(splits(0))
Dim ips() As System.Net.IPAddress = hostname.AddressList
output = path.Remove(2, splits(0).Length)
Return output.Insert(2, ips(0).ToString)
Else
Return path
End If
End Function

Thanks
Robert

"Roidy" <(E-Mail Removed)> wrote in message
news:0_QMn.5693$QF7.3830@hurricane...
> Thanks Armin,
>
> VB complains that GetHostByName is obsolete and tells you to use
> GetHostEntry, which works perfectly:-
>
> Dim hostname As System.Net.IPHostEntry =
> System.Net.Dns.GetHostEntry("NAS")
> Dim ipaddress() As System.Net.IPAddress = hostname.AddressList
>
> Now it just an easy task to write a function to strip out the network
> device name from the path and replace it with the resulting IP address.
>
> Thanks
> Robert
>
>
> "Armin Zingler" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Am 31.05.2010 14:56, schrieb Roidy:
>>> I have a function that reads a file but fails if I pass it a network
>>> device
>>> name instead of an ip address eg:-
>>>
>>> ReadFunction("\\NAS\Myfolder\Myfile.txt") <--- Fails
>>> ReadFunction("\\192.168.2.2\Myfolder\Myfile.txt") <--- Works
>>>
>>> So how do I convert a network devices name to it's ip address?
>>> ie. Convert \\NAS\ to \\192.168.2.2\

>>
>>
>> Try this: (passing "NAS")
>>
>> System.Net.Dns.GetHostByName(ByVal String) As System.Net.IPHostEntry
>>
>> Does it fail, too?
>>
>>
>> --
>> Armin
>>

>

 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      1st Jun 2010
Am 31.05.2010 17:55, schrieb Roidy:
> Thanks Armin,
>
> VB complains that GetHostByName is obsolete and tells you to use
> GetHostEntry, which works perfectly:-
>
> Dim hostname As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("NAS")
> Dim ipaddress() As System.Net.IPAddress = hostname.AddressList
>
> Now it just an easy task to write a function to strip out the network device
> name from the path and replace it with the resulting IP address.


Yes, sorry, you're right. Didn't try it and didn't see the warning.

--
Armin
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      1st Jun 2010
Am 31.05.2010 18:18, schrieb Roidy:
> This is what I knocked together to check the path to see if it's network
> path and then replace the device name with it's IP address. Seems to work,
> can you see any problems I might face using this? or is there a better way?


If it works fine for you, it's ok. :-) Though I wonder why your "ReadFunction"
works with the IP but not with the host name, while resolving the host name
before using GetHostEntry seems to work. What does 'ReadFunction" do with
the string? Apart from this:

- A possible problem is that the host name can not be resolved. I can't test
the situation because my provider seems to redirect all unknown host names to
the same IP.
- always enable Option Strict
- make the function Shared


> Public Function PathFixer(ByVal path As String) As String
> If path.StartsWith("\\") Then ' Check to see if it's a network path
> Dim output As String
> Dim splits() As String = path.Substring(2, path.Length -
> 2).Split("\")
> Dim hostname As System.Net.IPHostEntry =
> System.Net.Dns.GetHostEntry(splits(0))
> Dim ips() As System.Net.IPAddress = hostname.AddressList
> output = path.Remove(2, splits(0).Length)
> Return output.Insert(2, ips(0).ToString)
> Else
> Return path
> End If
> End Function


--
Armin
 
Reply With Quote
 
Roidy
Guest
Posts: n/a
 
      1st Jun 2010
Unfortunately the readfunction isn't mine it's the MP4Read function from
libmp4v2.dll so I'm not sure why it only works with IP addresses and not
host names. Your right I should really do some error checking because if
GetHostEntry fails to resolve the host name it throws an exception.

Thanks
Robert


"Armin Zingler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Am 31.05.2010 18:18, schrieb Roidy:
>> This is what I knocked together to check the path to see if it's network
>> path and then replace the device name with it's IP address. Seems to
>> work,
>> can you see any problems I might face using this? or is there a better
>> way?

>
> If it works fine for you, it's ok. :-) Though I wonder why your
> "ReadFunction"
> works with the IP but not with the host name, while resolving the host
> name
> before using GetHostEntry seems to work. What does 'ReadFunction" do with
> the string? Apart from this:
>
> - A possible problem is that the host name can not be resolved. I can't
> test
> the situation because my provider seems to redirect all unknown host names
> to
> the same IP.
> - always enable Option Strict
> - make the function Shared
>
>
>> Public Function PathFixer(ByVal path As String) As String
>> If path.StartsWith("\\") Then ' Check to see if it's a network
>> path
>> Dim output As String
>> Dim splits() As String = path.Substring(2, path.Length -
>> 2).Split("\")
>> Dim hostname As System.Net.IPHostEntry =
>> System.Net.Dns.GetHostEntry(splits(0))
>> Dim ips() As System.Net.IPAddress = hostname.AddressList
>> output = path.Remove(2, splits(0).Length)
>> Return output.Insert(2, ips(0).ToString)
>> Else
>> Return path
>> End If
>> End Function

>
> --
> Armin
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to convert email address to web address? Victor Delta Microsoft Excel Misc 4 13th Oct 2010 11:13 PM
Only one usage of each socket address (protocol/network address/port)is normally permitted pbd22 Microsoft C# .NET 18 3rd Feb 2008 12:56 AM
how can i convert an email address to a web address =?Utf-8?B?QXJ2aW5kIFNpa2Fy?= Microsoft Excel Worksheet Functions 3 3rd Oct 2006 08:06 PM
Convert file to binary, send it over the network, and convert it b =?Utf-8?B?Q2hld2ll?= Microsoft Dot NET 0 9th Sep 2005 06:04 PM
Convert file to binary, send it over the network, and convert it b =?Utf-8?B?Q2hld2ll?= Microsoft Dot NET Framework 1 9th Sep 2005 06:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:07 AM.