Checking if IPv6 is enabled on Windows 7 using C# - c#

I am trying to write a program using C# to act as a multipurpose tool for my company. One of the things we would like in this tool is to determine if IPv6 is enabled/binded to the local area connection network adapter on our Windows 7 machines. I'm not looking for it to have an address, just to know if it enabled or disabled on that adapter. I am unsure as to how to code this. From what I've been able to find online, it seems I should be using System.Net.Configuration and Ipv6Element to check if it is enabled, but I have no idea how to code it. I would like to be able to display if it is enabled or disabled in a text box, so I'm guessing I'd use Boolean values. Could someone point me in the right direction on this? Thanks!

You can test whether the OS supports IPv6 by using this property:
bool supportsIpV6 = System.Net.Sockets.Socket.OSSupportsIPv6;

You can query for exactly what you asked for (if IPv6 is enabled or disabled for a specific network adapter) with the following code using the System.Net.NetworkInformation namespace:
using System.Net.NetworkInformation;
// ...
NetworkInterface[] allInterfaces = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface firstInterface = allInterfaces[0];
bool interfaceSupportsIPv6 = firstInterface.Supports(NetworkInterfaceComponent.IPv6);
Documentation on MSDN: Link

I have used this code to test it. Notice that it tests if the IPV6 is enabled, and not if the Network Card is IPV6 compatible:
public static bool InterfaceHasIpv6Enabled(NetworkInterface #interface)
{
try
{
var properties = #interface.GetIPProperties().GetIPv6Properties();
return properties.Index > -999;
}
catch (System.Net.NetworkInformation.NetworkInformationException)
{
return false;
}
catch (Exception ex)
{
throw ex;
}
}

Related

Connect to Always-On VPN using C#

This is my first time writing a post, so bear with me.
I've made a program for my work and have been asked to implement a feature in it now, where our users are able to connect to our VPN (Always-On VPN), using the program.
However, I don't seem to be able to find any information on how to actually establish the connection programmatically. I need to basically be able to click on a button that says "Connect to VPN" and it will then establish the connection, much like it does in Windows when clicking connect:
Clicking Connect in Windows
I've figured out how to see if the connection is established or not via my program:
public bool CheckForVPNInterface()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface #interface in interfaces)
{
if (#interface.Description.Contains("Always-On VPN") && #interface.OperationalStatus == OperationalStatus.Up)
{
Label_Connection.BackColor = Color.Green;
return true;
}
else
Label_Connection.BackColor = Color.Red;
}
}
return false;
}
But now just need to have the "Connect to VPN" button.
Any idea on how to do so?
NOTE: I've tried using rasdial, but that didn't work
I couldn't find much info on the subject, but I found this obscure Windows API: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/get_connected/getvpnconnected
Edit: maybe this will also help

Disable specific USB Storage Device C#

I am working on a project that needs to enable/disable specific USB storage devices.
My app gets all storage USB DeviceIDs, and depending on saved settings then needs to allow the device or not. I have used the code from this previous question for the enable/disable, and call it like so:
DisableHardware.DisableDevice(n => n.ToUpperInvariant().Contains(VidPid), true);
with string VidPid = "VID_8564&PID_1000";.
I have stepped through the code, it all works perfectly (as far as I can tell) until the SetupDIChangeState call, which then returns -536870389 (E000020B as hex) as error code (from Marshall.GetLastWin32Error()).
Apparently this refers to either 1) the device not being present (which as far as I understand is not the case here, as all other calls in this class work fine, and I get `VidPid' from
private static List<WMUBClasses.USBDeviceInfo> GetUSBDevices()
{
try
{
List<WMUBClasses.USBDeviceInfo> tList = new List<WMUBClasses.USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(#"SELECT * FROM Win32_USBHub"))
{
collection = searcher.Get();
}
foreach (var device in collection)
{
if (!device.GetPropertyValue("Description").ToString().Contains("Storage"))
{
continue;
}
tList.Add(new WMUBClasses.USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
return tList;
}
catch (Exception ex)
{
return null;
}
}
or 2) an incorrect build platform, but I have tried all the different combinations (Any CPU, Mixed Platforms, x86 and x64), they all return the same result.
I have also looked at this which is another approach to my problem (by creating and using a kernel mode filter driver), it just seems like killing a fly with a wrecking ball. To be honest I have no clue of how to go about using this (for someone that hasn't done any driver development it looks super intimidating, especially after having read some of the available documentation.)
Should I (A) keep using the SetupDi API calls to achieve my goal and if so, can anyone see what is wrong with the code or how I am using it? If not (A), should I (B) use the filter driver approach instead and if so, any pointers?
As stated in the header, I want to disable specific USB storage devices, so as far as I understand, this precludes using the Registry to disable ALL USB storage devices. So if neither of the above, does anyone have any other direction I should be looking at instead?

Lan connection is up or down

What is the way to find out if the LAN connection is up or down in wince 7 with c++ or c#?
You can use ipConfig command line tool from your c# or c++ application, it provides the status of all network adapters.
This question may be useful: Checking network status in C#
bool networkUp
= System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
Try this:
try
{
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName("hostname");
// found host
}
catch(System.Net.Socket.SocketException)
{
//host not found == LAN not connected!
}
You can use NetworkChange class in .NET. For Lan connection, use NetworkInterface.GetIsNetworkAvailable() method.
Return Value
Type: System.Boolean
true if a network connection is available; otherwise, false.
Also take a look those;
System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged

How can I check for 3G, wifi, EDGE, Cellular Networks in Windows Phone 7?

How can I check for 3G, wifi, EDGE, Cellular Networks in Windows Phone 7 using C#?
If you can use the Mango (7.1) SDK, and if your scenario involves using sockets, there's a trivial way to get the NetworkInterfaceType/SubType information for the connection you just made:
NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface();
var type = netInterfaceInfo.InterfaceType;
var subType = netInterfaceInfo.InterfaceSubtype;
No need to use the NetworkInterface.NetworkInterfaceType property (which notoriously takes up to 30sec to return); no need to trigger a hostname resolution just to determine the network type; no need to listen to network change events.
Of course, this works best in conjunction with DeviceNetworkInformation.IsNetworkAvailable or NetworkInterface.GetIsNetworkAvailable() - those calls return immediately whether you're on a network or not. If you are, you connect the socket first and ask questions when it's connected :-)
A final note: beware of Mango's DeviceNetworkInformation.IsWiFiEnabled - I thought it would return whether I was on a wifi network, but instead it returns whether wifi is turned on or off in the phone settings... not super useful.
take a look at phoney tools, they have class PhoneNetworking for this:
http://wildermuth.com/2011/03/05/Phoney_Tools_Updated_(WP7_Open_Source_Library)
its open source you can check the source code
As of the Mango release (beta 2 and RC), this information is now available but it requires you to actually make a connection, presumably because it doesn't check until something needs it.
You can either perform a DNS resolution (see below) or use the GetCurrentNetworkInterface WebRequest extension method, which will throw an InvalidOperationException if the request hasn't connected yet.
There are also some events to follow in the Microsoft.Phone.Net.NetworkInformation namespace, but I wouldn't be surprised if those events didn't fire until a connection was made.
Interestingly, it seems you can also prefer or require on a per-connection basis using the SetNetworkPreference and SetNetworkRequirement extension methods, though it doesn't go beyond wifi vs cellular.
DeviceNetworkInformation.ResolveHostNameAsync(
new DnsEndPoint("microsoft.com", 80),
new NameResolutionCallback(nrr =>
{
var info = nrr.NetworkInterface;
var type = info.InterfaceType;
var subType = info.InterfaceSubtype;
}), null);
The enumeration values for NetworkInterfaceType (wifi/gsm) and NetworkInterfaceSubType (edge/3g) are available on MSDN.
Without socket:
var currentList = new NetworkInterfaceList().Where(i => i.InterfaceState == ConnectState.Connected).Select(i => i.InterfaceSubtype);
if (currentList.Contains(NetworkInterfaceSubType.WiFi))
Debug.WriteLine("WiFi");
if (currentList.Intersect(new NetworkInterfaceSubType[]
{
NetworkInterfaceSubType.Cellular_EVDO,
NetworkInterfaceSubType.Cellular_3G,
NetworkInterfaceSubType.Cellular_HSPA,
NetworkInterfaceSubType.Cellular_EVDV,
}).Any())
Debug.WriteLine("3G");
if (currentList.Intersect(new NetworkInterfaceSubType[]
{
NetworkInterfaceSubType.Cellular_GPRS,
NetworkInterfaceSubType.Cellular_1XRTT,
NetworkInterfaceSubType.Cellular_EDGE,
}).Any())
Debug.WriteLine("2G");
Unfortunately the api's don't provide very limited information about the kind of network connection you have. You can tell if you are on 3G, Cellular or Ethernet (i.e. USB connection to PC) but that is all the information you get.
Check out this for more info Better way to check for an network connection on WP7
To get Network Data for windows phone app i.e it is connected to a ethernet, wifi or cellular network also getting the subtype i.e 2G or 3g network following program can be used.
Using Microsoft.Phone.Net.NetworkInformation
Using Microsoft.Phone.net.NetworkInfromation
var Newlist = new NetworkInterfaceList();
foreach (NetworkInterfaceInfo x in Newlist)
{
if(x.InterfaceState==ConnectState.Connected)
{
if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.WiFi))
{
Interface = x.InterfaceType.ToString();
SubInterface = x.InterfaceSubtype.ToString();
break;
}
else if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EVDO) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_3G) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_HSPA) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EVDV))
{
Interface = x.InterfaceType.ToString();
SubInterface= “3G Network”;
break;
}
else if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_GPRS) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_1XRTT) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EDGE))
{
Interface = x.InterfaceType.ToString();
SubInterface= “2G Network”;
break;
}
else
{
Interface = “Ethernet”;
SubInterface= “Unknown” ;
break;
}
}
else
{
Interface=”not connected”;
SubInterface=”unknown”;
}
Here, Interface and SubInterface gives the network information.

How do i set the dns search suffix for a network adapter in .net?

I've written a command line utility that detects which network interface is connected, and sets the staitc ip address and dns servers for it (by calling netsh). However, I can't seem to figure out how to set the dns search suffixes. netsh doesnt appear capable of doing that. How do I do that otherwise (WMI perhaps)?
I think you have to set the value(s) you want in the DNSDomainSuffixSearchOrder property of the Win32_NetworkAdapterConfiguration WMI object.
Here's and example of setting values in WMI, if you need it:
Modifying Objects & Running Methods
The dns search suffixes are valid for the whole machine, not for a single network adapter. You can also get them from registry:
string searchList = "";
try
{
using (var reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(tcpSettingsSubKey))
{
searchList = (reg.GetValue("SearchList") as string);
}
}
catch(Exception ex)
{
// something went wrong
}
(This is not the default dns suffix when the machine is an AD member)

Categories

Resources