Get DHCP IP address of Raspbery Pi - c#

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
}

Related

C# multi-hop SSH (SSH through SSH)

I wrote a C# console program to connect from A (Windows 10, Console C# app) over SSH to B (Linux server) and from there on to C (Linux server), but I cannot connect from B to C (from A to B is ok).
When I connect from A over Windows terminal to B and from B's terminal to C, it works, so I proved that my credentials are fine.
I am using Renci.SshNet for C#
I created a class Server with a .Connect(), .Disconnect() and .Execute() extension methods and then the two class instances Broker and Destination
My code looks like:
if (Broker.Connect())
{
Broker.Execute("pwd");
if (Destination.Connect())
{
Destination.Execute("pwd");
Destination.Disconnect();
}
Broker.Disconnect();
}
The Ssh connection objets are created like var broker = new SftpClient("Ip", Port, "User", "Pass")
Then I am internally using broker.Connect() and broker.Disconnect() in Renci.Ssh.Net lib given methods
To broker.Execute("cmd") I basically do
var output = host.Ssh.RunCommand(str);
var res0 = output.ExitStatus;
var res1 = output.Result;
var res2 = output.Error;
My code works for the first part as I manage to get the output of Broker.Execute("pwd") but it does not connect on Destination.Connect() returning the message A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
My aim ist to multi-hop using an automated process from within C#: users must not interact with any console and I cannot modify nor store any files on the Linux sites
Any idea where the problem lies?
Thanks in advance,
I will like to summarize here how I ended solving this issue with the help of some valuable hints gathered over the net and from #jeb:
Open a cmd.exe console, type ssh userC#hostC -p portC-J userB#hostB - p portB (portB and portC can be ommited if they are the default port 22) and then you will be promped to enter passwordB and passwordC - in this order.
If the connection succeeded you will be then on the hostC console and will manage to do whatever you intend to do.
The code you'll need is:
static void RunSshHop(params string[] cmds)
{
try
{
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
UseShellExecute = false,
//WorkingDirectory = #"d:\" // dir of your "cmd.exe"
};
p.OutputDataReceived += p_DataReceived;
p.ErrorDataReceived += p_DataReceived;
p.Start();
// way 1: works
foreach (var e in cmds)
p.StandardInput.Write($"{e}\n"); // cannot use 'WriteLine' because Windows is '\r' and Linux is '\n'
/* way 2: works as well
using (var sw = p.StandardInput)
{
foreach (var e in cmds)
if (sw.BaseStream.CanWrite)
sw.Write($"{e}\n"); // cannot use 'WriteLine' because Windows is '\r' and Linux is '\n'
}
//*/
p.WaitForExit();
if (p.HasExited)
{
Console.WriteLine($"ExitCode: {p.ExitCode}");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
And you can call it like this:
static void Main(string[] args)
{
RunSshHop(
"ssh userC#hostC -p portC-J userB#hostB - p portB",
"pwd",
//"...",
"ls"
);
}
To avoid having to enter the passwords for each host, you can also create an SSH key pair like this:
open cmd.exe console
type ssh-keygen -t rsa
choose path where to save the public and private keys that are to be generated (press enter to use the default destination)
copy the destination, you will need it later to get back yxour keys :-)
to manage an automated process, you have to leave the passphrase empty
-once the keys are generated, log onto the first host over ssh like ssh youruser#firsthost -p hostport (the -p hostport part can be ignored if port is the default 22)
type ssh-copy-id youruser#firsthost -p hostport
accept
repeat the process for the second host

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!

How to get default IP address when multiple IP addresses are assigned to PC

How can one get the default IP address excluding the 127.0.0.1 loopback address when mutliple IP addresses are assigned to PC i.e if the PC is multihomed.
Following code returns correct default IP address on one PC but returns incorrect IP address on another PC, so there must be some other solution.
private string[] GetDefaultIPWithSubnet()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string[] ipSubnet = new string[2];
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"])
{
try
{
string[] ips = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
ipSubnet[0] = ips[0].ToString();
ipSubnet[1] = subnets[0].ToString();
break;
}
catch (Exception ex)
{
return null;
}
}
}
return ipSubnet;
}
public static void GetDefaultIp()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
if (adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
foreach (var x in properties.UnicastAddresses)
{
if (x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
Console.WriteLine(" IPAddress ........ : {0:x}", x.Address.ToString());
}
}
}
}
I think you mean the interface with the default route. You can get the IPv4 route table with the GetIpForwardTable function (quick google reveals that it is callable through p/invoke) and look for a 0.0.0.0 destination route (run route print at command line to check what a route table looks like).
I think you misunderstood the meaning of IPEnabled, as far as I know that parameter is TRUE if TCP/IP is enabled on the interface. So I don't think this is what you're looking for.

Getting Serial Port Information

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()
);

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);
}

Categories