Parsa Karami`s Blog

an Enthusiastic Windows Delevoper

Set IPv4 Programmatically in Windows Iot

1488 days ago on August 26, 2017   |   Parsa Karami on Universal Windows App   |   3017
     Set IPv4 Programmatically in Windows Iot     

Changing the IP interface in my application programmatically is the one of my daily life problems when I develop universal windows 10 application and specially work with windows 10 iot core, if you want a way for change IP interface in your app follow this post, I will give you a convenient way to do that.

After install microsoft windows 10 iot core on the microSD card you must put it on your mini-computer board like Raspberry Pi or other boards. (i use RPI for my projects so the default board of my posts is RaspberryPi) so after put microSD card which installed windows 10 iot core on it, turn the device on and connect it with Ethernet to a network. if you do that and connect board to a display device or get remote with Windows Iot Remote Client Application  from device you see the below picture.


In specified red box on the view you see current IPAddress of windows iot device, if you don`t have a display device or cannot access the remote application in order to see the current IPAddress of windows you can use Windows 10 IoT Core Dashboard, you see the screenshot of this application below and download it from this link.


In the Windows 10 Iot Core Dashboard appliction the connected devices appear in the list, and you can see your board and it`s IPAddress, now copy the IPAddress and write it in browser with following pattern " Http://[Your Device IP Address]:8080 ", after write this url in your browser a login form open the windows iot asks you Username and Password in order to get secure access to windows device portal.


After login you can see the Windows Device Portal and it`s facilities to change system setting and monitor system state and use other functionalities through out Windows Device Portal.


You can see the functionalities of windows iot in each section on the left side of portal, each function map to an API and when you use it throgh out portal windows iot call these APIs from itself. See the windows device core API in this page.

For changing IPAddress of windows iot I can extract the address of API with Telerik Fiddler when that called from portal, for example for setting new IPAddress on device you must use this API : " Http://[Your Device IP Address]:8080/Api/Network/Ipv4config ".
This API verb is Put and it needs Authentication header that fill with username and password which use for login in windows device portal as i described earlier.Now for changing IPAddress must call this API from UWP application that run in windows iot, in this sample you see textbox that get custome IPAddress from user and a button that set IPAddress on device, also you see a button that get current IPAddress of windows iot and show it on a textblock which is located under it. After add authentication header, you must create a string with json format that use in put API for changing network interface properties.

1th step : craete an object from HttpClient class

     HttpClient client = new HttpClient();
     client.BaseAddress = new System.Uri("", UriKind.RelativeOrAbsolute);

2th step : add authentication header in order to get access api

     client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
     string.Format("{0}:{1}", "administrator", "p@ssw0rd"))));

3th step : define a class for create json object that represent the network interface

     public class NetworkConfiguration
          public string AdapterName { get; set; }
          public string IPAddress { get; set; }
          public string SubnetMask { get; set; }
          public string DefaultGateway { get; set; }
          public string PrimaryDNS { get; set; }
          public string SecondryDNS { get; set; }

4th step : get Adapter Name from windows device portal and put your customized value for setting new network configuration on windows iot with creating new object from NetworkConfiguration class

     NetworkConfiguration networkConfiguration = new NetworkConfiguration()
          AdapterName = "{CF2A43ED-F038-4411-BB0D-EA71D5604B5E}",
          IPAddress = "",
          SubnetMask = "",
          DefaultGateway = "",
          PrimaryDNS = "",
          SecondryDNS = ""

5th step : convert object to json string 

     string json = JsonConvert.SerializeObject(networkConfiguration);

6th step : create a httpContent for sending through api

     HttpContent content = new StringContent(json);

7th step : use PutAsync method in order to put value to api for edit current value

     var result = await client.PutAsync("api/networking/ipv4config", content);

8th step : use GetLocalIPAddress method for get current ipv4 address

     private static string GetLocalIPAddress()
               var icp = NetworkInformation.GetInternetConnectionProfile();
               if (icp?.NetworkAdapter == null) return null;
               var hostname = NetworkInformation.GetHostNames().SingleOrDefault(
                              hn => hn.IPInformation?.NetworkAdapter != null &&
                              hn.IPInformation.NetworkAdapter.NetworkAdapterId == icp.NetworkAdapter.NetworkAdapterId);
               return hostname?.CanonicalName;
          catch (Exception ex)
               return "an Error occured";

You can also get the project file and fork it on Github.