How do I get the local IP address of a system? [duplicate] - vb.net

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get local ip address using vb?
I am using Silverlight3 with VB.NET. I have written the follwing code in my service file. When I run my project locally, it gives the IP address of my system as 192.168.1.12 successfully. But, when I host my project it wont return the IP adress of the system which I am using.
ie: If i host my project in system A which is of ip 192.168.1.12 and if I access the project from system B which is of ip 192.168.1.7, the output of my project will gives the IP of system A (where i have hosted my project). It should give the IP of system B where am running it. Similarly, if am accessing the project from system C means it should give the IP of system C, but here it is returning the IP of system A.
Kindly help me please.
Thanks
Private Function fnLocalIp() As String
Try
Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
Dim strLocalIp As String = h.AddressList.GetValue(0).ToString
Return strLocalIp
Catch ex As Exception
End Try
End Function

You can get local ip using below code:
in vb.net:
Dim host As String = System.Net.Dns.GetHostName()
Dim LocalHostaddress As String = System.Net.Dns.GetHostByName(host).AddressList(1).ToString()
In c#:
string host = System.Net.Dns.GetHostName();
string LocalHostaddress = System.Net.Dns.GetHostByName(host).AddressList[1].ToString();

use dns Class , more information here
http://msdn.microsoft.com/it-it/library/system.net.dns.aspx
Regards.

Related

unable to connect to default instance using it's full name

I am developing an app that needs to get from a user a server name or address and a db name and build a folder structure based on that. The problem is that in order to have the same folder for all the different ways there are to get the path to the instance (localhost, ip address, etc.) i'm running the following Query:
select cast(SERVERPROPERTY('MachineName')as varchar)+'\'+##servicename
on the target server and set my folder structure based on that, and in that format saves the connection that the user gave (doesn't matter if i got ip or a server name, there is one connection string for all).
My problem is that when the instance is the default instance on the target machine, I cant seem to connect using MACHINE_NAME\MSSQLSERVER. I can only log in using the machine without instance name. So I need to either find a way to connect to the instance using its full name (preferred) or to find a way figure out if the targeted instance is the default one.
Any help would be very appreciated.
'MSSQLSERVER' is reserved for default instance, so you can
SELECT CAST(SERVERPROPERTY('MachineName')AS VARCHAR)+ CASE WHEN CHARINDEX('MSSQLSERVER', ##SERVICENAME, 1) >0 THEN '' ELSE '\'+##SERVICENAME END

Is it necessary that Data Source of connection string must match the system name

This is my first post to this precious website. I am a new learner of vb.net. I am working on a simple purchase project, where i got some errors. But the first thing is which baffled me is:
This is my connection string at module level, on the developed machine.
Public strCn As String = "Data Source = (local); Initial Catalog = PSys; Integrated Security = false; User ID = sa; Password = 123;"
Is it mandatory that Data Source must be the original name of the System Name. I mean If i use (local) or using ( . ), so will it work or not? Because when i copy my project to any other system for further development so every time i need to change the Data source, otherwise i get the error that: "Network-related or instance-specific error occurred......."
Kindly guide me that what i need to do.
When you are developing an application which uses a database server such as MsSQL it is not wise to install the server along with your application in every pc which is installed to. For example what are you going to do if a customer has a local network with 10 computers? Are you going to install SQL server in all 10 of them? And if so what if they need to share data?
So your best approach (based on common practice by other applications) will be to allow the user to install the SQL server where he wants and let him configure your application and point it to the server's location. If you follow that path then the configuration of your application can be in the setup application or in the application itself.
Now about the development phase, I had a similar situation in which I needed to develop the same application in two different computers. What I did was to install the SQL server in both of them with a named instance "sqlexpress" then in the application I used the
Data.SqlClient.SqlConnectionStringBuilder
class to build the connection string. I did something like this:
Public Function getDevConnectionString() As String
Dim csb As New Data.SqlClient.SqlConnectionStringBuilder(My.Settings.dbConnectionString) '<-My original cs in app settings
csb.DataSource = My.Computer.Name & "\sqlexpress"
Return csb.ConnectionString
End Function
Whenever I need a connection string I simply call getDevConnectionString() which returns the connection string based on the computer name plus the sql server instance name. For example:
Dim cs As String
#If DEBUG Then
cs = getDevConnectionString()
#Else
cs = getReleaseConnectionString()
#End If
where getReleaseConnectionString() is the function that returns your connection string configured by the customer.
Hope this point you the right direction...

How to open a Lotus Notes Database on a network share?

I am trying to interact with a Notes database (my personal mail file), via C# and am stuck right out of the gate, trying to open the database with the following code:
string sNotesSourceFolder = "$Inbox";
string sUserName = "scady";
string sMailFile = #"NotesMail_FDrive\ARchives\" + sUserName + ".nsf";
string sServerName = #"Answers1/Answers";
string sPassword = "xxxxxx";
//Creating the notes session
NotesSession session = new NotesSession();
session.Initialize(sPassword);
NotesDatabase NotesDb = session.GetDatabase(sServerName, sMailFile, false);
//If the database is not already open then open it.
if (!NotesDb.IsOpen)
{
NotesDb.Open();
}
calling session.GetDatabase() with the server name and path the the nsf file returns null which, apparently means it could not open the db.
I am running this code locally, against a Notes server called "Answers1/Answers". I am now thinking based on From the Notes server, the nsf files are located on a network fileshare of "NotesMail_FDrive\ARchives\xyz.nsf".
Is the server on which the file resides a Notes server or just a network share?
If it's a network share, you need the drive mapping, but sServerName would be "" to get to a "local" drive. I'm assuming Notesdata1 is a directory on X, so if it's the drive name, remove it.
string sMailFile = #"X:\Notesdata1\NotesMail\ARchives\scady.nsf";
string sServerName = #"";
If it's a Notes server AND Notesdata1 is the data directory for the Notes server, then
string sMailFile = #"NotesMail\ARchives\scady.nsf";
string sServerName = #"Answers1/Answers";
Sorry I didn't notice this on the first pass...
The value for sMailFile should not be "\10.10.10.71\notesdata1\NotesMail\ARchives\scady.nsf". The other two values that you have commented out are also wrong. The arguments for getDatabase are the Domino server's distinguished name, and a relative path. No server IP, no volume, and no "file:" prefix. Just something like this: "archives\scady.nsf". It should be exactly what you see for the path when you open up the database in your Notes client and bring up the database properties dialog.
Previous Answer
Your sServername variable contains the Domino server's distinguished name, as it should.
But why are you using File.Exists? The Domino server wants exclusive access to all of the files in its data tree. If you are actually successfully accessing the file on the Domino server via a filesystem call, then perhaps you are locking the Domino server out of accessing it.
But also: where are you running this code? Are you running it on the same computer, and under the same account that you normally use to read your email? Or on a different computer and account?

VB.NET : Grabbing and Tieing IP Addresses to Interfaces

I would like to find a way to grab the assigned IPv4 and IPv6 addresses to different interfaces and being able to determine which interface they're tied to.
Currently I am looping ' System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces ' and able to grab interface specific information:
For Each nic As System.Net.NetworkInformation.NetworkInterface In System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
myarr(count2, 0) = (String.Format("{0}", nic.Description))
myarr(count2, 1) = (String.Format("{0}", nic.GetPhysicalAddress))
myarr(count2, 2) = (String.Format("{0}", nic.OperationalStatus))
myarr(count2, 3) = (String.Format("{0}", nic.Speed))
count2 += 1
Next
Through this method I can't determine a simple method to grab assigned IP Addresses unfortunately.
The most common method that I can easily find with google-foo is to find your IP via going through something like the following:
Dim ipAdd As IPAddress = Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.First(Function(f) f.AddressFamily = Sockets.AddressFamily.InterNetwork)
Which is fine and dandy but I can't determine which interface is being used nor can it determine an assigned IP Address that's not going through DNS or mulitple NICS.
I can possibly do some ghetto method of issuing a command-line command and parsing all the information out of ipconfig or getting a GUID and running through the registry but I feel like there should be an easier more effecient method.
A good example of what I want is the ability to produce a list like IPCONFIG where it has interface information and their designated IP Addresess, etc...
in you for-loop try
Dim ip = nic.GetIPProperties().UnicastAddresses(0).Address;

How to resolve the domain server name from .NET

Hi
In a single server Windows domain, does anyone know how to get the servername.
For example, in my test environment, we have a single Windows Small Business Server 2008 called horatio, it is the domain controller for longtest.local.
I have played around with the system.dns (with no positive results) :
Dim myIP As IPHostEntry = Dns.GetHostEntry(My.Computer.Name)
Dim IPAddress As String = myIP.AddressList.GetValue(0).ToString
Dim myFullDomainName = Dns.GetHostEntry(IPAddress).HostName
Dim DomainParts() As String = myFullDomainName.Split("."c)
Dim DomainName As String = DomainParts(1)
However, I cannot seem to grab the servername, I need a function like :
GetDomainServer() that will return horatio (from the example above.
Driving me a bit mad this one.
Regards
Mark Harby
Nottingham. UK
There's an environment variable called LOGONSERVER that you can use:
var domainServerName = Environment.GetEnvironmentVariable("LOGONSERVER");
Ask the server. Like environment variable. The local machine name is available in one environment variable.