2

I am doing a task to retrieve the Wi-fi details. I have gone through Microsoft documentation and found a UWP wi-fi scan file and ran the code, it gave all details.

But I need the namespace windows.devices.wifi in the console app, I can't import that. So I tried with NetworkInterface class, and it gave some details, but the Security status is missing.

public static void networkInterface()
        {
            bool results = NetworkInterface.GetIsNetworkAvailable();
            if (results)
            {
                Console.WriteLine("Connected.\n");

                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in interfaces.Where(x => x.OperationalStatus == OperationalStatus.Up))
                {
                    if (adapter.Name[0] == 'E')
                    {
                        var props = adapter.GetIPProperties();
                        var result = props.UnicastAddresses.FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork);
                        Console.WriteLine("Ethernet Details\n");
                        if (result != null)
                        {
                            Console.WriteLine("Name: {0}", adapter.Name);
                            Console.WriteLine("Status: {0}", adapter.OperationalStatus);
                            Console.WriteLine("MAC: {0}", adapter.GetPhysicalAddress());



                            var ip = result.Address.ToString();
                            Console.WriteLine("IP Address: {0}", ip);


                            var subnet = result.IPv4Mask.ToString();
                            Console.WriteLine("Subnet Mask: {0}\n", subnet);
                        }
                    }

                    if (adapter.Name.ToLower() == "wi-fi")
                    {
                        Console.WriteLine("Wi-Fi Details");
                        using (PowerShell PowerShellInstance = PowerShell.Create())
                        {
                            Runspace rs = RunspaceFactory.CreateRunspace();
                            rs.Open();
                            Pipeline pipeline = rs.CreatePipeline();
                            pipeline.Commands.AddScript("netsh wlan show interfaces");
                            pipeline.Commands.Add("Out-string");
                            Collection<PSObject> re = pipeline.Invoke();
                            rs.Close();

                            StringBuilder sb = new StringBuilder();
                            foreach (PSObject obj in re)
                                sb.AppendLine(obj.ToString());

                            Console.WriteLine(sb.ToString());

                        }
                    }
                }
            }
            else
                Console.WriteLine("Not Connected.");
        } 

This code worked by running the PowerShell operation. Can you suggest any method by directly doing in C#. Reason why I need in C# is, Ethernet part runs faster because it directly runs in C#, Wi-fi part is slow.

Vijay
  • 31
  • 3

0 Answers0