I have an program in production environment where I like to have a window to open, when a remote assistance are started on the pc, so the person connecting to the pc have some more options. But i can't find anything if this is possible? If so any idea how to detect it?
This can be done but I find it tricky and I generally avoid this. See How to detect RDC from C#.net for more info.
To start RDP listens on port 3389 so something like this should work.
int port = 3389;
using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(#"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp", false))
{
if (key != null)
{
object value = key.GetValue("PortNumber");
if (value != null) port = Convert.ToInt32(value);
}
}
But the port number can be configured so this isn't the best way.
Then there is Pinvoke and Cassia. with Cassia you could do something like:
public bool IsComputerUsedByTS()
{
var tsMgr = new TerminalServicesManager();
var localSvr = tsMgr.GetLocalServer();
var sessions = localSvr.GetSessions();
foreach(var session in sessions)
{
if(session.ConnectionState == ConnectionState.Active ||
session.ConnectionState == ConnectionState.Connected) //Add more states you want to check for as needed
{
return true;
}
}
return false;
}
And last but not least:
System.Windows.Forms.SystemInformation.TerminalServerSession
This uses a forms import but is a very simple solution. If you run your program in a remote desktop environment, this returns true.
Related
I wanted to check whether my phone can connect to Internet or not. I have seen several questions already. One of those is Question . It says to use NetworkInterface.GetIsNetworkAvailable() this and i tried it. I have disconnected my PC from internet and Also turned off the DataConnection of the emulator but NetworkInterface.GetIsNetworkAvailable() this always returning true. But simultaneouly i also check for NetworkInterfaceType.None and interestingly it is coming null.
Can anybody explain where i am missing info ?
Attempt : -
public static void CheckNetworkAvailability()
{
// this is coming true even when i disconnected my pc from internet.
// i also make the dataconnection off of the emulator
var fg = NetworkInterface.GetIsNetworkAvailable();
var ni = NetworkInterface.NetworkInterfaceType;
// this part is coming none
if (ni == NetworkInterfaceType.None)
IsConnected = false;
}
any help is appreciated :)
I am using following code to check if the device has access to internet with if it is connected to wifi or data connection..
public void UpdateNetworkInformation()
{
// Get current Internet Connection Profile.
ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
//air plan mode is on...
if (internetConnectionProfile == null)
{
Is_Connected = false;
return;
}
//if true, internet is accessible.
this.Is_InternetAvailable = internetConnectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
// Check the connection details.
else if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != 71)// Connection is not a Wi-Fi connection.
{
Is_Roaming = internetConnectionProfile.GetConnectionCost().Roaming;
/// user is Low on Data package only send low data.
Is_LowOnData = internetConnectionProfile.GetConnectionCost().ApproachingDataLimit;
//User is over limit do not send data
Is_OverDataLimit = internetConnectionProfile.GetConnectionCost().OverDataLimit;
}
else //Connection is a Wi-Fi connection. Data restrictions are not necessary.
{
Is_Wifi_Connected = true;
}
}
Edit:
And for simple internet connection you can use below code.
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
Hope this helps!
The emulator always returns NetworkInterface.GetIsNetworkAvailable() as true, even if you emulate network conditions as having no network.
I faced this problem myself and the only truly way of testing this behaviour is to deploy the application to a physical device running Windows Phone and test it with the data turned off.
NetworkInterface.GetIsNetworkAvailable() checks the network connection and not the internet connection. If you are in any kind of network, then it will return true whether internet is present or not.
You can check the internet connection as following:
using System.Net
private bool IsOnline()
{
try
{
IPHostEntry iPHostEntry = Dns.GetHostEntry("www.wikipedia.com");
return true;
}
catch (SocketException ex)
{
return false;
}
}
Is there a way in C# to find the IP address of a server that I'm connecting to on a specific port?
I know the port will always be 28961, and I want to get the IP address of the server that I'm connecting to on this port.
I have written a program that does something similar. I used the SharpPcap Assemblies. The code below should be able to get you started:
private void StartCapture(ICaptureDevice device)
{
// Register our handler function to the
// 'packet arrival' event
device.OnPacketArrival += new SharpPcap.PacketArrivalEventHandler(device_OnPacketArrival);
// Open the device for capturing
int readTimeoutMilliseconds = 1000;
device.Open(DeviceMode.Normal, readTimeoutMilliseconds);
device.Filter = "";
// Start the capturing process
device.StartCapture();
}
private void device_OnPacketArrival(object sender, CaptureEventArgs e)
{
var packet = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
var ip = PacketDotNet.IpPacket.GetEncapsulated(packet);
if (ip != null)
{
int destPort = 0;
if (ip.Protocol.ToString() == "TCP")
{
var tcp = PacketDotNet.TcpPacket.GetEncapsulated(packet);
if (tcp != null)
{
destPort = tcp.DestinationPort;
}
}
else if (ip.Protocol.ToString() == "UDP")
{
var udp = PacketDotNet.UdpPacket.GetEncapsulated(packet);
if (udp != null)
{
destPort = udp.DestinationPort;
}
}
int dataLength = e.Packet.Data.Length;
string sourceIp = ip.SourceAddress.ToString();
string destIp = ip.DestinationAddress.ToString();
string protocol = ip.Protocol.ToString();
}
}
by implementing your own if statements you should be able to get what you need by using the code above.
This CodeProject article may help you. It links to a fully working demo project download. It's been around for a long time and no doubt there are easier ways to do some of this in later versions of .NET. But it still works and should give you what you need.
Once you've got the list of active TCP/IP connections you should have everything you need to pick out the one on port 28961 and grab the IP address.
I'm writing a program that shows the user their IP address, Subnet mask and Default gateway. I can get the first two, but for the last one, this is what I turned up:
GatewayIPAddressInformationCollection gwc =
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().GatewayAddresses;
That, of course, returns a collection of GatewayIPAddressInformation. So, if a computer has multiple gateways, how can I determine which is the default gateway?
In practice, I've only ever seen this collection contain a single entry, but since it's implemented as a collection, it stands to reason that some computers contain multiple gateways, none of which are marked as "Default". So is there a way to determine the default or is it all just guesswork?
It will probably be the first valid and enabled gateway address of the first enabled network interface:
public static IPAddress GetDefaultGateway()
{
return NetworkInterface
.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up)
.Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.SelectMany(n => n.GetIPProperties()?.GatewayAddresses)
.Select(g => g?.Address)
.Where(a => a != null)
// .Where(a => a.AddressFamily == AddressFamily.InterNetwork)
// .Where(a => Array.FindIndex(a.GetAddressBytes(), b => b != 0) >= 0)
.FirstOrDefault();
}
I've also added some further commented checks which have been pointed out as useful by other people here. You can check the AddressFamily one to distinguish between IPv4 and IPv6. The latter one can be used to filter out 0.0.0.0 addresses.
The above solution will give you a valid/connected interface, and is good enough for 99% of situations. That said, if you have multiple valid interfaces that traffic can be routed through, and you need to be 100% accurate, the way to do this uses GetBestInterface to find an interface for routing to a specific IP address. This additionally handles the case where you might have a specific address range routed through a different adapter (e.g. 10.*.*.* going through a VPN, everything else going to your router)
[DllImport("iphlpapi.dll", CharSet = CharSet.Auto)]
private static extern int GetBestInterface(UInt32 destAddr, out UInt32 bestIfIndex);
public static IPAddress GetGatewayForDestination(IPAddress destinationAddress)
{
UInt32 destaddr = BitConverter.ToUInt32(destinationAddress.GetAddressBytes(), 0);
uint interfaceIndex;
int result = GetBestInterface(destaddr, out interfaceIndex);
if (result != 0)
throw new Win32Exception(result);
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
{
var niprops = ni.GetIPProperties();
if (niprops == null)
continue;
var gateway = niprops.GatewayAddresses?.FirstOrDefault()?.Address;
if (gateway == null)
continue;
if (ni.Supports(NetworkInterfaceComponent.IPv4))
{
var v4props = niprops.GetIPv4Properties();
if (v4props == null)
continue;
if (v4props.Index == interfaceIndex)
return gateway;
}
if (ni.Supports(NetworkInterfaceComponent.IPv6))
{
var v6props = niprops.GetIPv6Properties();
if (v6props == null)
continue;
if (v6props.Index == interfaceIndex)
return gateway;
}
}
return null;
}
These two examples could be wrapped up into a helper class and used in the appropriate cases: that you do, or do not have a destination address in mind already.
The first IP address returned by traceroute command will be the gateway .You can use this fact for getting gateway.A nice implementation of tracerout can be found here:
TraceRoute and Ping in C#
according to #midspace's comment on #caesay's answer this is a better answer:
public static IPAddress GetDefaultGateway()
{
var gateway_address = NetworkInterface.GetAllNetworkInterfaces()
.Where(e => e.OperationalStatus == OperationalStatus.Up)
.SelectMany(e => e.GetIPProperties().GatewayAddresses)
.FirstOrDefault();
if (gateway_address == null) return null;
return gateway_address.Address;
}
I'm warning that it's not a complete solution, if your'e looking for the main interface responsible for the internet access, you should combine other approaches like using win32 API GetBestInterface to find the best interface to connect to a destination address.
you can find example usage here: http://www.pinvoke.net/default.aspx/iphlpapi.getbestinterface
I've just come across this and will be using the following code - basically it looks for interfaces that aren't loopback and are up and have gateways and unicast addresses. From the interface I then get a non transient IPV4 address.
public static class NetworkInterfaces
{
public static NetworkInterface GetDefaultInterface()
{
var interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (var intf in interfaces)
{
if (intf.OperationalStatus != OperationalStatus.Up)
{
continue;
}
if (intf.NetworkInterfaceType == NetworkInterfaceType.Loopback)
{
continue;
}
var properties = intf.GetIPProperties();
if (properties == null)
{
continue;
}
var gateways = properties.GatewayAddresses;
if ((gateways == null) || (gateways.Count == 0))
{
continue;
}
var addresses = properties.UnicastAddresses;
if ((addresses == null) || (addresses.Count == 0))
{
continue;
}
return intf;
}
return null;
}
public static IPAddress GetDefaultIPV4Address(NetworkInterface intf)
{
if (intf == null)
{
return null;
}
foreach (var address in intf.GetIPProperties().UnicastAddresses)
{
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
{
continue;
}
if (address.IsTransient)
{
continue;
}
return address.Address;
}
return null;
}
}
I know this is a slightly older question but, I had just come upon the need to retrieve the IPV4 address of the local gateway. The accepted answer doesn't quite fit the bill when it comes to my own system so, I modified it to suite and I'm sure others will be able to use this solution, too.
Since I don't yet have enough rep to comment, I'm forced to add this as a "question":
public static IPAddress GetDefaultGateway()
{
IPAddress result = null;
var cards = NetworkInterface.GetAllNetworkInterfaces().ToList();
if (cards.Any())
{
foreach (var card in cards)
{
var props = card.GetIPProperties();
if (props == null)
continue;
var gateways = props.GatewayAddresses;
if (!gateways.Any())
continue;
var gateway =
gateways.FirstOrDefault(g => g.Address.AddressFamily.ToString() == "InterNetwork");
if (gateway == null)
continue;
result = gateway.Address;
break;
};
}
return result;
}
Been looking for one weird trick to determine the local IP address?
Yet do it very robustly vs. the earlier methods?
[See end for response to #caesay]
This may be what you are looking for.
using (var s = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp))
{
s.Bind( new System.Net.IPEndPoint(System.Net.IPAddress.Any,0));
s.Connect("google.com",0);
var ipaddr = s.LocalEndPoint as System.Net.IPEndPoint;
var addr = ipaddr.Address.ToString();
Console.WriteLine(addr);
}
That will create a UDP socket, but will not send data on it.
You can then see what local interface and address the socket was bound to.
You must supply an IP address or hostname where I used google.com, that will
be used by the OS to determine which interface the socket will be bound to,
and what IP address you will find using this method.
For 99%+ of IP addresses or hostnames you use, you will get the interface+address to be used for traffic over the default route.
While this is a little obtuse, I suspect it is more reliable than the methods above where people use the PInvoke call and scan through the structures to see if they can find the interface.
I certainly found it more robust on my 3x double interface systems than scanning through the results of NetworkInterface.GetAllNetworkInterfaces() using heuristics to try to figure out the default interface address.
[Update/response to #caesay 2/6/2019]
#caesay Has made an excellent point here, so I have switched the sample from using UdpClient to Socket, and explicitly called Socket.Connect() after Bind(). I have also reviewed the Framework source for Connect() and LocalEndPoint, and both call their corresponding Win32 OS system call immediately without deferring or being lazy about calling into the OS. The Win32 bind page says "The application can use getsockname after calling bind to learn the address and the port that has been assigned to the socket. If the Internet address is equal to INADDR_ANY or in6addr_any, getsockname cannot necessarily supply the address until the socket is connected".
This condition is now explicitly addressed, and having reviewed the Framework source, the IP address should always be available after the Connect() call.
NetworkInterface[] allNICs = NetworkInterface.GetAllNetworkInterfaces();
foreach (var nic in allNICs)
{
var ipProp = nic.GetIPProperties();
var gwAddresses = ipProp.GatewayAddresses;
if (gwAddresses.Count > 0 &&
gwAddresses.FirstOrDefault(g => g.Address.AddressFamily == AddressFamily.InterNetwork) != null)
{
localIP = ipProp.UnicastAddresses.First(d => d.Address.AddressFamily == AddressFamily.InterNetwork).Address;
}
}
Debug.Print(localIP.ToString());
(Just after posting I noticed an earlier answer that mentioned the idea of using traceroute. I probably wouldn't have responded if I'd seen that. But I have included complete/compact code.)
Here's a different approach that finds the default gateway by using a traceroute type method. Ping an address on the internet (or other network you want to get to) with a TTL of 1, and let ping tell you what the first hop IP address was. That ought to be the gateway you're looking for.
I suppose this could go wrong if the gateway was configured not to reply to pings, but I don't think I've ever heard of a network set up that way.
Note: This routine kind-of even works if you pass it an IPV6 address. The potential issue is that it seems to return an IPV6 address with Global scope, rather than Link-Local scope. I don't have enough experience using IPV6 to know if that's good or bad. But I do know that the Windows ipconfig command displays the Link-Local IPV6 addresses of the interface gateway.
using System.Net
using System.Net.NetworkInformation
// ping internet address with ttl=1 and return address of host where ttl expired
public static IPAddress FindDefaultGateway(IPAddress netaddr = null)
{
// user can provide an ip address that exists on the network they want to connect to,
// or this routine will default to 1.1.1.1 (IP of a popular internet dns provider)
if (netaddr is null)
netaddr = IPAddress.Parse("1.1.1.1");
PingReply reply = default;
using var ping = new Ping();
var options = new PingOptions(1, true); // ttl=1, dont fragment=true
try {
// I arbitrarily used a 200ms timeout; tune as you see fit.
reply = ping.Send(netaddr, 200, new byte[0], options);
}
catch (PingException) {
System.Diagnostics.Debug.WriteLine("Gateway not available");
return default;
}
if (reply.Status != IPStatus.TtlExpired) {
System.Diagnostics.Debug.WriteLine("Gateway not available");
return default;
}
return reply.Address;
}
I think that you should iterate over this collection and show all the Gateways, since if there are many gateways to an adapter they are all considered Default to that adapter
Is there a way in c# to check if the app is running on localhost (as opposed to a production server)?
I am writing a mass mailing program that needs to use a certain mail queue is it's running on localhost.
if (Localhost)
{
Queue = QueueLocal;
}
else
{
Queue = QueueProduction;
}
As a comment has the correct solution I'm going to post it as an answer:
HttpContext.Current.Request.IsLocal
What about something like:
public static bool OnTestingServer()
{
string host = HttpContext.Current.Request.Url.Host.ToLower();
return (host == "localhost");
}
Use a value in the application configuration file that will tell you what environment you are on.
Since you are using asp.net, you can utilize config file transforms to ensure the setting is correct for each of your environments.
See if this works:
public static bool IsLocalIpAddress(string host)
{
try
{ // get host IP addresses
IPAddress[] hostIPs = Dns.GetHostAddresses(host);
// get local IP addresses
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
// test if any host IP equals to any local IP or to localhost
foreach (IPAddress hostIP in hostIPs)
{
// is localhost
if (IPAddress.IsLoopback(hostIP)) return true;
// is local address
foreach (IPAddress localIP in localIPs)
{
if (hostIP.Equals(localIP)) return true;
}
}
}
catch { }
return false;
}
Reference: http://www.csharp-examples.net/local-ip/
Localhost ip address is constant, you can use it to determines if it´s localhost or remote user.
But beware, if you are logged in the production server, it will be considered localhost too.
This covers IP v.4 and v.6:
public static bool isLocalhost( )
{
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
return (ip == "127.0.0.1" || ip == "::1");
}
To be totally sure in which server the code is running at, you can use the MAC address:
public string GetMACAddress()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
String sMacAddress = string.Empty;
foreach (NetworkInterface adapter in nics)
{
if (sMacAddress == String.Empty)// only return MAC Address from first card
{
IPInterfaceProperties properties = adapter.GetIPProperties();
sMacAddress = adapter.GetPhysicalAddress().ToString();
}
} return sMacAddress;
}
from: http://www.c-sharpcorner.com/uploadfile/ahsanm.m/how-to-get-the-mac-address-of-system-using-Asp-NetC-Sharp/
And compare with a MAC address in web.config for example.
public static bool isLocalhost( )
{
return GetMACAddress() == System.Configuration.ConfigurationManager.AppSettings["LocalhostMAC"].ToString();
}
Unfortunately there is no HttpContext.HttpRequest.IsLocal() anymore within core.
But after checking the original implementation in .Net, it is quite easy to reimplement the same behaviour by checking HttpContext.Connection:
private bool IsLocal(ConnectionInfo connection)
{
var remoteAddress = connection.RemoteIpAddress.ToString();
// if unknown, assume not local
if (String.IsNullOrEmpty(remoteAddress))
return false;
// check if localhost
if (remoteAddress == "127.0.0.1" || remoteAddress == "::1")
return true;
// compare with local address
if (remoteAddress == connection.LocalIpAddress.ToString())
return true;
return false;
}
Or, you could use a C# Preprocessor Directive if your simply targeting a development environment (this is assuming your app doesn't run in debug in production!):
#if debug
Queue = QueueLocal;
#else
Queue = QueueProduction;
just like this:
HttpContext.Current.Request.IsLocal
string hostName = Request.Url.Host.ToString();
I know this is the really old thread but still, someone looking for a straight solution then you can use this:
if (HttpContext.Current.Request.Url.Host == "localhost")
{
//your action when app is running on localhost
}
This is an alternative, more transparent, option:
public static bool IsLocal
{
// MVC < 6
get
{
var authority = HttpContext.Request.Url.Authority.ToLower();
return authority == "localhost" ||
authority.StartsWith("localhost:");
}
// MVC 6+
get
{
return String.Compare(HttpContext.Request.Url.Host, "localhost",
StringComparison.OrdinalIgnoreCase);
}
}
If you're not doing this in the Controller then add Current after HttpContext, as in HttpContext.Current.Request...
Also, in MVC 6, in the View, HttpContext is just Context
What is the best way to check if the UDP port is open or not on the same machine. I have got port number 7525UDP and if it's open I would like to bind to it. I am using this code:
while (true)
{
try {socket.bind()}
catch (Exception ex)
{MessageBox.Show("socket probably in use");}
}
but is there a specified function that can check if the UDP port is open or not. Without sweeping the entire table set for UDP ports would be also good.
int myport = 7525;
bool alreadyinuse = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners().Any(p => p.Port == myport);
A comment below suggested a variation which would supply the first free UDP port... however, the suggested code is inefficient as it calls out to the external assembly multiple times (depending on how many ports are in use). Here's a more efficient variation which will only call the external assembly once (and is also more readable):
var startingAtPort = 5000;
var maxNumberOfPortsToCheck = 500;
var range = Enumerable.Range(startingAtPort, maxNumberOfPortsToCheck);
var portsInUse =
from p in range
join used in System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()
on p equals used.Port
select p;
var FirstFreeUDPPortInRange = range.Except(portsInUse).FirstOrDefault();
if(FirstFreeUDPPortInRange > 0)
{
// do stuff
Console.WriteLine(FirstFreeUDPPortInRange);
} else {
// complain about lack of free ports?
}