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
Related
I just got myself into using the SerialPort object in C# and I realised it throws an exception saying that "COM1" does not exist.
I checked my device manager to see what COM ports I can use, but is there a way to find out what COM ports are available and programmatically select one of them?
Yes, use SerialPort.GetPortNames(), which returns an array of strings of available port names.
Then create your SerialPort object by specifying one of the names in the constructor.
string[] ports = SerialPort.GetPortNames();
SerialPort port = new SerialPort(ports[0]); // create using first existing serial port, for example
One-liner :
if(SerialPort.GetPortNames().ToList().Contains(comportName))
{
port = new SerialPort(comportName)
}
Here is another way
string portExists = SerialPort.GetPortNames().Any(x => x == "COM1");
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;
}
}
Is it possible to add a virtual network interface with Java or C#. I need to set a bunch of IPs to my computer for a data mining app and I don't have enought NICs (I don't want to buy more yet).
I need full control of virtual cards from the app (create, delete, set IP, maybe redirect traffic).
I am not sure you have a pure Java solution for this.
You have several alternatives.
1. Use a script that will do that for you, writing this solution in perl\shell will be 4-5 lines.
2. You can open a ssh connection to your machine and run the commands from Java.
I have such utility that uses trilead-ssh2.
To get it using maven you can use:
<dependency>
<groupId>com.trilead</groupId>
<artifactId>trilead-ssh2</artifactId>
<version>build213-svnkit-1.3-patch</version>
</dependency>
For example, this way you should open a connection:
public static Connection newConnectionWithPassword(String host, String username, String passwd) {
Connection newConn = new Connection(host);
try {
newConn.connect(); // Ignoring ConnectionInfo returned value.
newConn.authenticateWithPassword(username, passwd);
return newConn;
} catch (IOException ioe) {
newConn.close();
ioe.printStackTrace();
return null;
}
}
Then to run your command you can :
this.session = conn.openSession();
this.session.execCommand(this.cmd);
You can use an OS command to create your virtual interfaces from java now.
BTW,
You can test the results using NetworkInterface.class , that can query a netweork interface on your machine.
Good luck.
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.
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)