How to get local IP address of Windows system? - windows

Is there any code or method to get the IP addresses of the local system?

To enumerate local IP addresses, use the Win32 API GetAdaptersInfo() (supports IPv4 only) or GetAdaptersAddresses() (supports IPv4 and IPv6) function. C/C++ examples are included in their documentation.

If you are in C#, you could use .NET:
using System;
using System.Net;
public static string GetLocalIP() {
var hosts = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ipEntry in host.AddressList)
{
if (ipEntry.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
}
You can later add a throw new Exception at the end, it will show up if you have no IPv4 adapters installed.

Related

IBM IOT C# client thowing invalid ip address exception when constructing gatewayclient

I am beginning to use the sample IBM-IOT C# sample code as per
https://github.com/ibm-watson-iot/iot-csharp/blob/master/docs/Gateway.rst
however I get "An invalid IP address was specified." thrown when the gateway constructor is called using the org id.
I'm using an orgid of 'p3wg4w' (set in config and accessed as a string property Globals.WatsonOrgID" )
my code is
private static void InitGatewayClient()
{
if (gw == null)
{
gw = new GatewayClient(Globals.WatsonOrgID,
Globals.WatsonGatewayDeviceType,
Globals.WatsonGatewayDeviceID,
Globals.WatsonAuthMethod,
Globals.WatsonToken);
gw.commandCallback += processCommand;
gw.errorCallback += processError;
gw.connect();
Console.WriteLine("Gateway connected");
Console.WriteLine("publishing gateway events..");
}
}
Has anyone seen this before ?
check if you can access or if you can:
telnet p3wg4w.messaging.internetofthings.ibmcloud.com 8883
The libraries aren't using any IP to create the connection, it is using the below vars
public static string DOMAIN = ".messaging.internetofthings.ibmcloud.com";
public static int MQTTS_PORT = 8883;
I can only think that your firewall is blocking the connection
I've used the below sample and worked just fine for me:
https://github.com/ibm-watson-iot/iot-csharp/blob/master/sample/Gateway/SampleGateway.cs

Get iOS device ip address in xamarin

hi can you please tell me how to access iOS device ip address using xamarin. we are building iOS app in which we want to show device local ip address. I used many other solutions but they didnt work for me.
I found a post about it here: https://forums.xamarin.com/discussion/348/acquire-device-ip-addresses-monotouch-since-ios6-0
it goes as follow:
Try using System.Net.NetworkInformation.NetworkInterface:
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces()) {
if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet) {
foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses) {
if (addrInfo.Address.AddressFamily == AddressFamily.InterNetwork) {
var ipAddress = addrInfo.Address;
// use ipAddress as needed ...
}
}
}
}

Can I use a build-in account to access different network drivers in IIS7?

I have to use one weird IIS7 web app which needs access to three network drivers: the first one is on NAS, the other two are shared folders on different Windows servers. But this weird IIS web app needs to use the same account to access all the three folders. So far, I have tried these combinations:
use NAS account to access NAS is IIS7: yes
use pass through account to access NAS in IIS7: yes
use windows account to access network driver in IIS7: yes
use pass through account to access network driver in IIS7: no
The NAS account has an ip prefix which Windows do not support, that is why I can not force all the network drives to use the same account.
I do not know why IIS7 can not use a pass through account (LocalService, LocalSystem, NetworService, or ApplicationPoolIdentify) to access net shared folder by Windows, while IIS7 can use a build-in account to access NAS. That does not make sense.
Please help!
When NAS and all the three servers in the same domain, this article can solve this problem ApplicationPoolIdentity user cannot modify files in shared folder in Windows Server 2008.
Go to the Shared Folder –> right click –> properties -> security –>edit –> add (so far as usual ) -> choose object types –> check on computers –> now enter the computer name where your application is working from , where you published your IIS application.
But there is no domain in my condition.
There are three ways to solve this problem:
1、Add the same windows account/same password in all the three network driver servers.
In IIS7, use this windows account as identity.
However NAS account requires ip prefix, while Windows account do not allow ip prefix. So I can not use this one.
2、Add the servers to the same domain, allow IIS7 server to access network drivers in all the network driver servers.
However, the end user do not allow me to do this.
3、At last I wrote another IIS7 web app,which helps the weird IIS7 web app logs in all there network drivers.
The end user have to access my web app once, before they can use the weird IIS7 web app.
In my web app, I used this API a lot: WNetAddConnection2.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
namespace IISReadDiskTest
{
public class NetworkDrive
{
public enum ResourceScope
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
}
public enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
}
public enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
}
public enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
}
[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
public ResourceScope dwScope = 0;
public ResourceType dwType = 0;
public ResourceDisplayType dwDisplayType = 0;
public ResourceUsage dwUsage = 0;
public string lpLocalName = null;
public string lpRemoteName = null;
public string lpComment = null;
public string lpProvider = null;
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NETRESOURCE lpNetResource, string lpPassword, string lpUsername, int dwFlags);
public static int MapNetworkDrive(string remotePath, string localDrive, string userName, string passWord)
{
NETRESOURCE myNetResource = new NETRESOURCE();
myNetResource.lpLocalName = localDrive;
myNetResource.lpRemoteName = remotePath;
myNetResource.lpProvider = null;
int result = WNetAddConnection2(myNetResource, passWord, userName, 0);
return result;
}
}
}

Clickonce Autodetect Proxy

I have created a C sharp Wpf ClickOnce application which uses xml rpc for communincation. A lot of my users get there proxy settings in different ways. Some use a pac file, other from IE or dhcp etc. I want to automate this whole process of getting the proxy details in any kind of environment. I have tried a LOT of different code snippets but want to hear if something like this already exists.
I see the Xml Rpc documentation has a setProxy method but I'm not sure how to specify the username or passsword if one is used. This whole process is still a little bit confusing for me.
I have also tried many different pieces of code including the WebProxy Class and using DefaultCredentials,DefaultProxy,GetSystemWebProxy etc.
At the moment I'm going to try a dllimport using winhttp to get the proxy settings. I am not sure if one can do this in a Clickonce Deployment. Is the dllimport the same as p/invoke ?
As you can see I would appreciate some advice on how to go about getting ANY type of proxy setting.
Appreciate any feedback.
ClickOnce installation/update doesn't really support proxy authentication. It will use the information in IE, and sometimes the machine.config file. The definitive thread with all known information about this is here.
I haven't had have problems with proxy authentication from the standpoint of installing applications. When using our application, which called backend WCF services, we let the user provide his proxy authentication information, and we applied the settings programmatically when making the service calls. This has nothing to do with ClickOnce.
This worked for me :
public static IExample ProxyAndCredentials { get; set; }
public static string ProxyUrl { get; set; }
public static void SetupProxyAndCredentials() {
//Insert your website here where XmlRpc calls should go
var url = new Uri("http://www.example.com/");
try
{
ProxyUrl = WebRequest.DefaultWebProxy.GetProxy(url).ToString();
Log.Debug(url + " is using proxy " + ProxyUrl);
if (ProxyUrl == url.ToString() || ProxyUrl == url + "/"){
// A proxy is not in use here
ProxyUrl = "";
Log.Debug("No proxy is used for " + url);
}
else if (!String.IsNullOrEmpty(ProxyUrl)){
// A proxy is in use
ProxyAndCredentials.Proxy = new WebProxy(ProxyUrl);
Log.Debug("A proxy is used for " + url);
}
//Set credentials, in my experience it is better to always set these
ProxyAndCredentials.Credentials = CredentialCache.DefaultNetworkCredentials;
ProxyAndCredentials.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
}
catch (Exception p)
{
//Handle Exception
}
}

Getting current IP of windows phone 7

I am building a windows phone 7.1 app. I need to find out if the phone is connected to any Wifi and if so, what is its current IP in the local network (ie. 192.168.0.100 like this).
I've been trying to find out these information for some time now. Please help.
I've managed to get the local IP on my console app by using the following code
public void ScanIP()
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
String localIP = ip.ToString();
Console.WriteLine(localIP);
}
}
Console.ReadKey();
}
However, I need similar thing done for windows mobile 7 app. Any idea ? Please share.
Do a multicast and listen for replies. Once you identify your multicast message, you can get the IP of the sender (which is yourself). You can use UdpAnySourceMulticastClient to do the multicasting. In case you're not in a wifi network, you will get a socket failure in the EndJoinGroup call. You should handle the exception and pass a specific value indicating you're not in a wifi network.
More info in this blog post by Andy Pennell.
it will provide you the ip address of the phone ...
public static IPAddress Find()
{
List<string> ipAddresses = new List<string>();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
if (hn.IPInformation != null)
{
string ipAddress = hn.DisplayName;
ipAddresses.Add(ipAddress);
}
}
IPAddress address = IPAddress.Parse(ipAddresses[0]);
return address;
}

Resources