Getting Serial Port Information - c#

I have some code that loads the serial ports into a combo-box:
List<String> tList = new List<String>();
comboBoxComPort.Items.Clear();
foreach (string s in SerialPort.GetPortNames())
{
tList.Add(s);
}
tList.Sort();
comboBoxComPort.Items.Add("Select COM port...");
comboBoxComPort.Items.AddRange(tList.ToArray());
comboBoxComPort.SelectedIndex = 0;
I would like to add the port descriptions (similar to what are shown for the COM ports in the Device Manager) to the list and sort the items in the list that are after index 0 (solved: see above snippet). Does anyone have any suggestions for adding the port descriptions? I am using Microsoft Visual C# 2008 Express Edition (.NET 2.0). Any thoughts you may have would be appreciated. Thanks.

I tried so many solutions on here that didn't work for me, only displaying some of the ports. But the following displayed All of them and their information.
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
{
var portnames = SerialPort.GetPortNames();
var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());
var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();
foreach(string s in portList)
{
Console.WriteLine(s);
}
}

EDIT: Sorry, I zipped past your question too quick. I realize now that you're looking for a list with the port name + port description. I've updated the code accordingly...
Using System.Management, you can query for all the ports, and all the information for each port (just like Device Manager...)
Sample code (make sure to add reference to System.Management):
using System;
using System.Management;
using System.Collections.Generic;
using System.Linq;
using System.IO.Ports;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (var searcher = new ManagementObjectSearcher
("SELECT * FROM WIN32_SerialPort"))
{
string[] portnames = SerialPort.GetPortNames();
var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
var tList = (from n in portnames
join p in ports on n equals p["DeviceID"].ToString()
select n + " - " + p["Caption"]).ToList();
tList.ForEach(Console.WriteLine);
}
// pause program execution to review results...
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
}
More info here: http://msdn.microsoft.com/en-us/library/aa394582%28VS.85%29.aspx

Use following code snippet
It gives following output when executed.
serial port : Communications Port (COM1)
serial port : Communications Port (COM2)
Don't forget to add
using System;
using System.Management;
using System.Windows.Forms;
Also add reference to system.Management (by default it is not available)
C#
private void GetSerialPort()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPEntity");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["Caption"].ToString().Contains("(COM"))
{
Console.WriteLine("serial port : {0}", queryObj["Caption"]);
}
}
}
catch (ManagementException e)
{
MessageBox.Show( e.Message);
}
}
VB
Private Sub GetAllSerialPortsName()
Try
Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity")
For Each queryObj As ManagementObject In searcher.Get()
If InStr(queryObj("Caption"), "(COM") > 0 Then
Console.WriteLine("serial port : {0}", queryObj("Caption"))
End If
Next
Catch err As ManagementException
MsgBox(err.Message)
End Try
End Sub
Update:
You may also check for
if (queryObj["Caption"].ToString().StartsWith("serial port"))
instead of
if (queryObj["Caption"].ToString().Contains("(COM"))

There is a post about this same issue on MSDN:
Getting more information about a serial port in C#
Hi Ravenb,
We can't get the information through the SerialPort type. I don't know why you need this info in your application. However, there's a solved thread with the same question as you. You can check out the code there, and see if it can help you.
If you have any further problem, please feel free to let me know.
Best regards,
Bruce Zhou
The link in that post goes to this one:
How to get more info about port using System.IO.Ports.SerialPort
You can probably get this info from a WMI query. Check out this tool to help you find the right code. Why would you care though? This is just a detail for a USB emulator, normal serial ports won't have this. A serial port is simply know by "COMx", nothing more.

None of the answers here satisfies my needs.
The answer from Muno is wrong because it lists ONLY the USB ports.
The answer from code4life is wrong because it lists all EXCEPT the USB ports. (Nevertheless it has 44 up-votes!!!)
I have an EPSON printer simulation port on my computer which is not listed by any of the answers here. So I had to write my own solution. Additionally I want to display more information than just the caption string. I also need to separate the port name from the description.
My code has been tested on Windows XP, 7, 10 and 11.
The Port Name (like "COM1") must be read from the registry because WMI does not give this information for all COM ports (EPSON).
If you use my code you do not need SerialPort.GetPortNames() anymore. My function returns the same ports, but with additional details. Why did Microsoft not implement such a function into the framework??
using System.Management;
using Microsoft.Win32;
using (ManagementClass i_Entity = new ManagementClass("Win32_PnPEntity"))
{
foreach (ManagementObject i_Inst in i_Entity.GetInstances())
{
Object o_Guid = i_Inst.GetPropertyValue("ClassGuid");
if (o_Guid == null || o_Guid.ToString().ToUpper() != "{4D36E978-E325-11CE-BFC1-08002BE10318}")
continue; // Skip all devices except device class "PORTS"
String s_Caption = i_Inst.GetPropertyValue("Caption") .ToString();
String s_Manufact = i_Inst.GetPropertyValue("Manufacturer").ToString();
String s_DeviceID = i_Inst.GetPropertyValue("PnpDeviceID") .ToString();
String s_RegPath = "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Enum\\" + s_DeviceID + "\\Device Parameters";
String s_PortName = Registry.GetValue(s_RegPath, "PortName", "").ToString();
int s32_Pos = s_Caption.IndexOf(" (COM");
if (s32_Pos > 0) // remove COM port from description
s_Caption = s_Caption.Substring(0, s32_Pos);
Console.WriteLine("Port Name: " + s_PortName);
Console.WriteLine("Description: " + s_Caption);
Console.WriteLine("Manufacturer: " + s_Manufact);
Console.WriteLine("Device ID: " + s_DeviceID);
Console.WriteLine("-----------------------------------");
}
}
I tested the code with a lot of COM ports. This is the Console output:
Port Name: COM29
Description: CDC Interface (Virtual COM Port) for USB Debug
Manufacturer: GHI Electronics, LLC
Device ID: USB\VID_1B9F&PID_F003&MI_01\6&3009671A&0&0001
-----------------------------------
Port Name: COM28
Description: Teensy USB Serial
Manufacturer: PJRC.COM, LLC.
Device ID: USB\VID_16C0&PID_0483\1256310
-----------------------------------
Port Name: COM25
Description: USB-SERIAL CH340
Manufacturer: wch.cn
Device ID: USB\VID_1A86&PID_7523\5&2499667D&0&3
-----------------------------------
Port Name: COM26
Description: Prolific USB-to-Serial Comm Port
Manufacturer: Prolific
Device ID: USB\VID_067B&PID_2303\5&2499667D&0&4
-----------------------------------
Port Name: COM1
Description: Comunications Port
Manufacturer: (Standard port types)
Device ID: ACPI\PNP0501\1
-----------------------------------
Port Name: COM999
Description: EPSON TM Virtual Port Driver
Manufacturer: EPSON
Device ID: ROOT\PORTS\0000
-----------------------------------
Port Name: COM20
Description: EPSON COM Emulation USB Port
Manufacturer: EPSON
Device ID: ROOT\PORTS\0001
-----------------------------------
Port Name: COM8
Description: Standard Serial over Bluetooth link
Manufacturer: Microsoft
Device ID: BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&000F\8&3ADBDF90&0&001DA568988B_C00000000
-----------------------------------
Port Name: COM9
Description: Standard Serial over Bluetooth link
Manufacturer: Microsoft
Device ID: BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\8&3ADBDF90&0&000000000000_00000002
-----------------------------------
Port Name: COM30
Description: Arduino Uno
Manufacturer: Arduino LLC (www.arduino.cc)
Device ID: USB\VID_2341&PID_0001\74132343530351F03132
-----------------------------------
COM1 is a COM port on the mainboard.
COM 8 and 9 are Buetooth COM ports.
COM 25 and 26 are USB to RS232 adapters.
COM 28 and 29 and 30 are Arduino-like boards.
COM 20 and 999 are EPSON ports.

I combined previous answers and used structure of Win32_PnPEntity class which can be found found here.
Got solution like this:
using System.Management;
public static void Main()
{
GetPortInformation();
}
public string GetPortInformation()
{
ManagementClass processClass = new ManagementClass("Win32_PnPEntity");
ManagementObjectCollection Ports = processClass.GetInstances();
foreach (ManagementObject property in Ports)
{
var name = property.GetPropertyValue("Name");
if (name != null && name.ToString().Contains("USB") && name.ToString().Contains("COM"))
{
var portInfo = new SerialPortInfo(property);
//Thats all information i got from port.
//Do whatever you want with this information
}
}
return string.Empty;
}
SerialPortInfo class:
public class SerialPortInfo
{
public SerialPortInfo(ManagementObject property)
{
this.Availability = property.GetPropertyValue("Availability") as int? ?? 0;
this.Caption = property.GetPropertyValue("Caption") as string ?? string.Empty;
this.ClassGuid = property.GetPropertyValue("ClassGuid") as string ?? string.Empty;
this.CompatibleID = property.GetPropertyValue("CompatibleID") as string[] ?? new string[] {};
this.ConfigManagerErrorCode = property.GetPropertyValue("ConfigManagerErrorCode") as int? ?? 0;
this.ConfigManagerUserConfig = property.GetPropertyValue("ConfigManagerUserConfig") as bool? ?? false;
this.CreationClassName = property.GetPropertyValue("CreationClassName") as string ?? string.Empty;
this.Description = property.GetPropertyValue("Description") as string ?? string.Empty;
this.DeviceID = property.GetPropertyValue("DeviceID") as string ?? string.Empty;
this.ErrorCleared = property.GetPropertyValue("ErrorCleared") as bool? ?? false;
this.ErrorDescription = property.GetPropertyValue("ErrorDescription") as string ?? string.Empty;
this.HardwareID = property.GetPropertyValue("HardwareID") as string[] ?? new string[] { };
this.InstallDate = property.GetPropertyValue("InstallDate") as DateTime? ?? DateTime.MinValue;
this.LastErrorCode = property.GetPropertyValue("LastErrorCode") as int? ?? 0;
this.Manufacturer = property.GetPropertyValue("Manufacturer") as string ?? string.Empty;
this.Name = property.GetPropertyValue("Name") as string ?? string.Empty;
this.PNPClass = property.GetPropertyValue("PNPClass") as string ?? string.Empty;
this.PNPDeviceID = property.GetPropertyValue("PNPDeviceID") as string ?? string.Empty;
this.PowerManagementCapabilities = property.GetPropertyValue("PowerManagementCapabilities") as int[] ?? new int[] { };
this.PowerManagementSupported = property.GetPropertyValue("PowerManagementSupported") as bool? ?? false;
this.Present = property.GetPropertyValue("Present") as bool? ?? false;
this.Service = property.GetPropertyValue("Service") as string ?? string.Empty;
this.Status = property.GetPropertyValue("Status") as string ?? string.Empty;
this.StatusInfo = property.GetPropertyValue("StatusInfo") as int? ?? 0;
this.SystemCreationClassName = property.GetPropertyValue("SystemCreationClassName") as string ?? string.Empty;
this.SystemName = property.GetPropertyValue("SystemName") as string ?? string.Empty;
}
int Availability;
string Caption;
string ClassGuid;
string[] CompatibleID;
int ConfigManagerErrorCode;
bool ConfigManagerUserConfig;
string CreationClassName;
string Description;
string DeviceID;
bool ErrorCleared;
string ErrorDescription;
string[] HardwareID;
DateTime InstallDate;
int LastErrorCode;
string Manufacturer;
string Name;
string PNPClass;
string PNPDeviceID;
int[] PowerManagementCapabilities;
bool PowerManagementSupported;
bool Present;
string Service;
string Status;
int StatusInfo;
string SystemCreationClassName;
string SystemName;
}

I'm not quite sure what you mean by "sorting the items after index 0", but if you just want to sort the array of strings returned by SerialPort.GetPortNames(), you can use Array.Sort.

this.comboPortName.Items.AddRange(
(from qP in System.IO.Ports.SerialPort.GetPortNames()
orderby System.Text.RegularExpressions.Regex.Replace(qP, "~\\d",
string.Empty).PadLeft(6, '0')
select qP).ToArray()
);

Related

C# WMI SetMTU System.ManagementException

We are working on a programm to configure some NICs.
We have to change IP Adresses, Subnetmask and the MTU.
Everything went well except the MTU Statement:
public void SetMTU()
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if (networkadapterID == (String)objMO["SettingID"])
{
ManagementBaseObject setMTU;
ManagementBaseObject newMTU = objMO.GetMethodParameters("SetMTU");
Int32 test = 9216;
newMTU["MTU"] = test;
setMTU = objMO.InvokeMethod("SetMTU", newMTU, null);
}
}
}
The correct NIC ID is given. Other WMI Operations succeed but we stuck on that one with error Message:
System.Management.ManagementException: "Die Methode ist ungültig. "
(System.Management.ManagementException: "The Method is invalid.")
We have also tried to use "test" as string or uint32 (because the microsoft docs says it's an uint32),also:
newMTU["MTU"] = new (u)int[] { MTU };
but it doesnt work either.
Meanwhile we don't have any ideas how to fix the problem.
I am grateful for every idea.
Thanks for your help and have a good day,
Alex
Edit:
Code to read the MTU should be (you have to tell this part a NetworkID so you don't read the Value of every NIC, you find this in your registry, but you should be able to delete the if part):
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if (networkadapterID == (String)objMO["SettingID"])
{
MessageBox.Show(Convert.ToString(objMO["MTU"]) + ": " + Convert.ToString(objMO["SettingID"]));
}
}
So far we didn't get the problem solved but we now check if the OS is Windows 10 and we will start a PowerShell task with this command:
Get-NetAdapterAdvancedProperty -Name 'NICName' -DisplayName 'Jumbo-Rahmen' | Set-NetAdapterAdvancedProperty -RegistryValue 'MTUsize';
Jumbo-Rahmen should be JumboPacket on an English OS
If the OS is not Windows 10 the User will be asked to change the MTU manually

Get DHCP IP address of Raspbery Pi

I have a .NetCore c# app.
I am using it in a Raspberry Pi device running Raspbian.
I am trying to get my assigned DHCP IP address.
I have tried many things.
They all return 127.0.0.1.
This is using web sockets. The server is written in c# and client is written in JS.
Any ideas apart from the usual examples out there?
Latest attempts:
public void GetIPAddress()
{
List<string> IpAddress = new List<string>();
var Hosts = System.Windows.Networking.Connectivity.NetworkInformation.GetHostNames().ToList();
foreach (var Host in Hosts)
{
string IP = Host.DisplayName;
IpAddress.Add(IP);
}
IPAddress address = IPAddress.Parse(IpAddress.Last());
Console.WriteLine(address);
}
Tells me that "The type or namespace name 'Networking' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)"
public static string GetLocalIPAddress()
{
var localIP = "";
try
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
Console.WriteLine(localIP);
//break;
}
}
}
catch ( Exception e )
{
Console.WriteLine( e );
Environment.Exit( 0 );
}
return localIP;
}
Returns 127.0.0.1
should also point out that using 127.0.0.1 as the web socket connection does not work for some reason
Rather than rely on .Net Core libraries/framework I instead googled linux commands to get the ip address as I know it does this.
If I open a Terminal window on the Pi and type in:
hostname -I
it will return the ip address.
So, my next step was to run this linux command from within C#.
For this I can use the process class and redirct the output:
//instantiate a new process with c# app
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "hostname", //my linux command i want to execute
Arguments = "-I", //the argument to return ip address
UseShellExecute = false,
RedirectStandardOutput = true, //redirect output to my code here
CreateNoWindow = true /do not show a window
}
};
proc.Start(); //start the process
while (!proc.StandardOutput.EndOfStream) //wait until entire stream from output read in
{
Console.WriteLine( proc.StandardOutput.ReadLine()); //this contains the ip output
}

how to retrieve IP v6 subnet mask length

I'm coding an application that has to get the network adapters configuration on a Windows 7 machine just like it's done in the Windows network adapters configuration panel:
So far I can get pretty much all the information I need from NetworkInterface.GetAllNetworkInterfaces() EXCEPT the subnet prefix length.
I'm aware that it can be retrieved from the C++ struc PMIB_UNICASTIPADDRESS_TABLE via OnLinkPrefixLength but I'm trying to stay in .net.
I also took a look at the Win32_NetworkAdapterConfiguration WMI class but it only seems to return the IP v4 subnet mask.
I also know that some information (not the prefix length, as far as I know) are in the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\TCPIP6\Parameters\Interfaces\{CLSID}
I also used SysInternals ProcessMon to try to get anything usefull when saving the network adapter settings but found nothing...
So, is there any clean .NET way to get this value? (getting it from the registry wouldn't be a problem)
EDIT: Gateways
This doesn't concern the actual question, but for those who need to retrieve the entire network adapter IPv6 configuration, the IPInterfaceProperties.GatewayAdresses property only supports the IPv4 gateways. As mentionned in the answer comments below, the only way to get the entire info until .NET framework 4.5 is to call WMI.
You can do so using Win32_NetworkAdapterConfiguration. You might have overlooked it.
IPSubnet will return an array of strings. Use the second value.
I didn't have time to whip up some C# code, but I'm sure you can handle it. Using WBEMTEST, I pulled this:
instance of Win32_NetworkAdapterConfiguration
{
Caption = "[00000010] Intel(R) 82579V Gigabit Network Connection";
DatabasePath = "%SystemRoot%\\System32\\drivers\\etc";
DefaultIPGateway = {"192.168.1.1"};
Description = "Intel(R) 82579V Gigabit Network Connection";
DHCPEnabled = TRUE;
DHCPLeaseExpires = "20120808052416.000000-240";
DHCPLeaseObtained = "20120807052416.000000-240";
DHCPServer = "192.168.1.1";
DNSDomainSuffixSearchOrder = {"*REDACTED*"};
DNSEnabledForWINSResolution = FALSE;
DNSHostName = "*REDACTED*";
DNSServerSearchOrder = {"192.168.1.1"};
DomainDNSRegistrationEnabled = FALSE;
FullDNSRegistrationEnabled = TRUE;
GatewayCostMetric = {0};
Index = 10;
InterfaceIndex = 12;
IPAddress = {"192.168.1.100", "fe80::d53e:b369:629a:7f95"};
IPConnectionMetric = 10;
IPEnabled = TRUE;
IPFilterSecurityEnabled = FALSE;
IPSecPermitIPProtocols = {};
IPSecPermitTCPPorts = {};
IPSecPermitUDPPorts = {};
IPSubnet = {"255.255.255.0", "64"};
MACAddress = "*REDACTED*";
ServiceName = "e1iexpress";
SettingID = "{B102679F-36AD-4D80-9D3B-D18C7B8FBF24}";
TcpipNetbiosOptions = 0;
WINSEnableLMHostsLookup = TRUE;
WINSScopeID = "";
};
IPSubnet[1] = IPv6 subnet;
Edit: Here's some code.
StringBuilder sBuilder = new StringBuilder();
ManagementObjectCollection objects = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration").Get();
foreach (ManagementObject mObject in objects)
{
string description = (string)mObject["Description"];
string[] addresses = (string[])mObject["IPAddress"];
string[] subnets = (string[])mObject["IPSubnet"];
if (addresses == null && subnets == null)
continue;
sBuilder.AppendLine(description);
sBuilder.AppendLine(string.Empty.PadRight(description.Length,'-'));
if (addresses != null)
{
sBuilder.Append("IPv4 Address: ");
sBuilder.AppendLine(addresses[0]);
if (addresses.Length > 1)
{
sBuilder.Append("IPv6 Address: ");
sBuilder.AppendLine(addresses[1]);
}
}
if (subnets != null)
{
sBuilder.Append("IPv4 Subnet: ");
sBuilder.AppendLine(subnets[0]);
if (subnets.Length > 1)
{
sBuilder.Append("IPv6 Subnet: ");
sBuilder.AppendLine(subnets[1]);
}
}
sBuilder.AppendLine();
sBuilder.AppendLine();
}
string output = sBuilder.ToString().Trim();
MessageBox.Show(output);
and some output:
Intel(R) 82579V Gigabit Network Connection
------------------------------------------
IPv4 Address: 192.168.1.100
IPv6 Address: fe80::d53e:b369:629a:7f95
IPv4 Subnet: 255.255.255.0
IPv6 Subnet: 64
Edit: I'm just going to clarify in case somebody searches for this later. The second item isn't always the IPv6 value. IPv4 can have multiple addresses and subnets. Use Integer.TryParse on the IPSubnet array value to make sure it's an IPv6 subnet and/or use the last item.
Parse the input stream of netsh with arguments:
interface ipv6 show route
Hope this helps!

Any way to turn the "internet off" in windows using c#?

I am looking for pointers towards APIs in c# that will allow me to control my Internet connection by turning the connection on and off.
I want to write a little console app that will allow me to turn my access on and off , allowing for productivity to skyrocket :) (as well as learning something in the process)
Thanks !!
If you're using Windows Vista you can use the built-in firewall to block any internet access.
The following code creates a firewall rule that blocks any outgoing connections on all of your network adapters:
using NetFwTypeLib; // Located in FirewallAPI.dll
...
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
firewallRule.Description = "Used to block all internet access.";
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = "Block Internet";
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
Then remove the rule when you want to allow internet access again:
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Remove("Block Internet");
This is a slight modification of some other code that I’ve used, so I can’t make any guarantees that it’ll work. Once again, keep in mind that you'll need Windows Vista (or later) and administrative privileges for this to work.
Link to the firewall API documentation.
This is what I am currently using (my idea, not an api):
System.Diagnostics;
void InternetConnection(string str)
{
ProcessStartInfo internet = new ProcessStartInfo()
{
FileName = "cmd.exe",
Arguments = "/C ipconfig /" + str,
WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(internet);
}
Disconnect from internet: InternetConnection("release");
Connect to internet: InternetConnection("renew");
Disconnecting will just remove the access to internet (it will show a caution icon in the wifi icon).
Connecting might take five seconds or more.
Out of topic:
In any cases you might want to check if you're connected or not (when you use the code above), I better suggest this:
System.Net.NetworkInformation;
public static bool CheckInternetConnection()
{
try
{
Ping myPing = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
return (reply.Status == IPStatus.Success);
}
catch (Exception)
{
return false;
}
}
There are actually a myriad of ways to turn off (Read: break) your internet access, but I think the simplest one would be to turn of the network interface that connects you to the internet.
Here is a link to get you started:
Identifying active network interface
Here's a sample program that does it using WMI management objects.
In the example, I'm targeting my wireless adapter by looking for network adapters that have "Wireless" in their name. You could figure out some substring that identifies the name of the adapter that you are targeting (you can get the names by doing ipconfig /all at a command line). Not passing a substring would cause this to go through all adapters, which is kinda severe. You'll need to add a reference to System.Management to your project.
using System;
using System.Management;
namespace ConsoleAdapterEnabler
{
public static class NetworkAdapterEnabler
{
public static ManagementObjectSearcher GetWMINetworkAdapters(String filterExpression = "")
{
String queryString = "SELECT * FROM Win32_NetworkAdapter";
if (filterExpression.Length > 0)
{
queryString += String.Format(" WHERE Name LIKE '%{0}%' ", filterExpression);
}
WqlObjectQuery query = new WqlObjectQuery(queryString);
ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher(query);
return objectSearcher;
}
public static void EnableWMINetworkAdapters(String filterExpression = "")
{
foreach (ManagementObject adapter in GetWMINetworkAdapters(filterExpression).Get())
{
//only enable if not already enabled
if (((bool)adapter.Properties["NetEnabled"].Value) != true)
{
adapter.InvokeMethod("Enable", null);
}
}
}
public static void DisableWMINetworkAdapters(String filterExpression = "")
{
foreach (ManagementObject adapter in GetWMINetworkAdapters(filterExpression).Get())
{
//If enabled, then disable
if (((bool)adapter.Properties["NetEnabled"].Value)==true)
{
adapter.InvokeMethod("Disable", null);
}
}
}
}
class Program
{
public static int Main(string[] args)
{
NetworkAdapterEnabler.DisableWMINetworkAdapters("Wireless");
Console.WriteLine("Press any key to continue");
var key = Console.ReadKey();
NetworkAdapterEnabler.EnableWMINetworkAdapters("Wireless");
Console.WriteLine("Press any key to continue");
key = Console.ReadKey();
return 0;
}
}
}
public static void BlockingOfData()
{
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN, NET_FW_ACTION_.NET_FW_ACTION_BLOCK);
firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, NET_FW_ACTION_.NET_FW_ACTION_BLOCK);
firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, NET_FW_ACTION_.NET_FW_ACTION_BLOCK);
}

Get SSID of the wireless network I am connected to with C# .Net on Windows Vista

I'd like to know if there is any .Net class that allows me to know the SSID of the wireless network I'm connected to.
So far I only found the library linked below. Is the best I can get or should I use something else?
Managed WiFi (http://www.codeplex.com/managedwifi)
The method that exploits WMI works for Windows XP but is it not working anymore with Windows Vista.
I resolved using the library. It resulted to be quite easy to work with the classes provided:
First I had to create a WlanClient object
wlan = new WlanClient();
And then I can get the list of the SSIDs the PC is connected to with this code:
Collection<String> connectedSsids = new Collection<string>();
foreach (WlanClient.WlanInterface wlanInterface in wlan.Interfaces)
{
Wlan.Dot11Ssid ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;
connectedSsids.Add(new String(Encoding.ASCII.GetChars(ssid.SSID,0, (int)ssid.SSIDLength)));
}
We were using the managed wifi library, but it throws exceptions if the network is disconnected during a query.
Try:
var process = new Process
{
StartInfo =
{
FileName = "netsh.exe",
Arguments = "wlan show interfaces",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
var line = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(l => l.Contains("SSID") && !l.Contains("BSSID"));
if (line == null)
{
return string.Empty;
}
var ssid = line.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1].TrimStart();
return ssid;
It looks like this will do what you want:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSNdis_80211_ServiceSetIdentifier");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSNdis_80211_ServiceSetIdentifier instance");
Console.WriteLine("-----------------------------------");
if(queryObj["Ndis80211SsId"] == null)
Console.WriteLine("Ndis80211SsId: {0}",queryObj["Ndis80211SsId"]);
else
{
Byte[] arrNdis80211SsId = (Byte[])
(queryObj["Ndis80211SsId"]);
foreach (Byte arrValue in arrNdis80211SsId)
{
Console.WriteLine("Ndis80211SsId: {0}", arrValue);
}
}
}
from http://bytes.com/groups/net-c/657473-wmi-wifi-discovery
there is some more information in How do I get the available wifi APs and their signal strength in .net?
(cross-posted in How to get currently connected wifi SSID in c# using WMI or System.Net.NetworkInformation windows 10?)
I found a rather old library dating back to 2014:
Microsoft.WindowsAPICodePack-Core version 1.1.0.2
Although it is not conforming to .NET Standard, this library integrates with my .NET Core 3.0 app, but obviously is not cross-platform.
Sample code:
var networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected);
foreach (var network in networks) {
sConnected = ((network.IsConnected == true) ? " (connected)" : " (disconnected)");
Console.WriteLine("Network : " + network.Name + " - Category : " + network.Category.ToString() + sConnected);
}
You are going to have to use native WLAN API. There is a long discussion about it here. Apparently this is what Managed Wifi API uses, so it will be easier for you to use it if you do not have any restrictions to use LGPL code.
I wanted to do exactly this, and tried using ManagedWifi, as suggested in other answers. But that led to unresolvable Exceptions as per here:
Issues with using Managed WiFi (NativeWiFi API)
I solved this by switching to using SimpleWiFi entirely and ignored the ManagedWifi package.
Glancing at the source code, it looks like SW is a fixed reimplementation of some of the functionality in MW.

Categories