Home > database >  Update IP Address Text Box If There Is A Change To The Network
Update IP Address Text Box If There Is A Change To The Network

Time:12-08

I'm trying to do something that is probably very simple, but I can't seem to get it working. I'm writing small app that displays some information including the IP Address. Everything is working perfectly, except that when the IP address changes (Network disconnect, LAN to WiFi, etc), I can't get it to update the text field with a message saying disconnected, or with the new IP Address. I've tried so many things and nothing works. A workaround that I am using is to shut the program down, and then start it immediately.

Here is the workaround code that I am using:

`

using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Management.Automation;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Threading;
using Microsoft.Win32;
using System.Diagnostics;
using System.Xml.Linq;
using System.Net;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.TeamFoundation.Common.Internal;

using Microsoft.TeamFoundation.Framework.Common;

namespace JIC_BackgroundInfo
{
    
    public partial class MainWindow : Window
    {
        private UserPreferenceChangedEventHandler UserPreferenceChanged;
        public MainWindow()
        {
            InitializeComponent();
          

            this.WindowStartupLocation = WindowStartupLocation.Manual;
            this.Left = System.Windows.SystemParameters.WorkArea.Width - this.Width;
            this.Top = System.Windows.SystemParameters.WorkArea.Height - this.Height;
            NetworkChange.NetworkAddressChanged  = new
            NetworkAddressChangedEventHandler(AddressChangedCallback);

            }


  static void AddressChangedCallback(object sender, EventArgs e)
        {
            Process.Start(@"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\JIC_BackgroundInfo.exe");
            Thread.Sleep(8500);
            Application.Current.Shutdown();

        }

` I tried the following code, along with many other variations, but it just crashes the app:

`

 public void AddressChangedCallback(object sender, EventArgs e)
        {
            using (PowerShell powerShell = PowerShell.Create())
            {
                try
                {
                    var ps1 = $@"(Get-NetIPAddress -AddressFamily IPv4 -AddressState Preferred -PrefixOrigin Dhcp).IPv4Address";

                    powerShell.AddScript(ps1);
                    Collection<PSObject> PSOutput = powerShell.Invoke();
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (PSObject pSObject in PSOutput)
                    {
                        stringBuilder.AppendLine(pSObject.ToString());
                    }

                    TxtBoxIPAddress.Text = stringBuilder.ToString();

                }

                catch { TxtBoxIPAddress.Text = "No Address Found!"; return; }
            }

        }

`

CodePudding user response:

Look into the ManagementEventWatcher class.

https://learn.microsoft.com/en-us/dotnet/api/system.management.managementeventwatcher?view=dotnet-plat-ext-7.0

You can use it to look for IP Addr changes and then ship those to your window that displays it.

CodePudding user response:

private void AddressChangedCallback(object sender, EventArgs e)
    {
       Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)delegate

       {            
                           
            using (PowerShell powerShell = PowerShell.Create())
        {
            try
            {
                var ps1 = $@"(Get-NetIPAddress -AddressFamily IPv4 -AddressState Preferred -PrefixOrigin Dhcp).IPv4Address";

                powerShell.AddScript(ps1);
                Collection<PSObject> PSOutput = powerShell.Invoke();
                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject pSObject in PSOutput)
                {
                    stringBuilder.AppendLine(pSObject.ToString());
                }

                TxtBoxIPAddress.Text = stringBuilder.ToString();

            }
            catch { TxtBoxIPAddress.Text = "No Address Found!"; }
        }
       });

    }
  • Related