failing to get IPv6 remotely - c#

I am trying to fetch IPs of remote machine. But my code fails to collect IPv6 remotely. The code runs fine when run locally. Following is my sample code
System.Net.IPHostEntry hostEntryComputer;
try
{
hostEntryComputer = System.Net.Dns.GetHostEntry(computerName);
foreach (System.Net.IPAddress addr in hostEntryComputer.AddressList)
{
string temp = addr.ToString();
Console.WriteLine("IP: " + temp);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
any help would be appreciated.

From MSDN: "IPv6 addresses are filtered from the results of the GetHostEntry method if the local computer does not have IPv6 installed. As a result, it is possible to get back an empty IPHostEntry instance if only IPv6 results where available for the hostNameOrAddress.parameter."
Follow those tutorials to enable it on your local machine:
Windows XP
Windows Vista/7

Related

Getting "The network path was not found" when trying to open remote cert store

I'm trying to get a list of non-expired certificates from the cert stores of remote machines. For some machines this works fine, but for others I'm getting the following error:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The network path was not found.
at Internal.Cryptography.Pal.StorePal.FromSystemStore(String storeName, StoreLocation storeLocation, OpenFlags openFlags)
at System.Security.Cryptography.X509Certificates.X509Store.Open(OpenFlags flags)
at GetCertificates(String server)
Here is the piece of code where this is coming from:
var store = new X509Store($#"\\{server}\My", StoreLocation.LocalMachine);
var certList = new List<X509Certificate2>();
try
{
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
certList = store.Certificates.Cast<X509Certificate2>()
.Where(x => x.NotBefore < DateTime.Now &&
DateTime.Now < x.NotAfter).ToList();
}
catch (Exception e)
{
throw;
}
finally
{
store.Close();
}
Any ideas why this maybe happening for some machines and/or possible workarounds/solutions?
Thank you
Based on the exception given, it is due to incorrect or in valid network path. Check whether you can access the remote server from your server where you have hosted the above code.
Found a solution. I ran my application as an administrator and now it seems to be able to resolve the network paths. I'm guessing we have some configuration/permission setup for these servers that only allow 'admins' to access the certs remotely.

Auto Detect The Application on LAN using C#.net

I have an Open Source Voice Chatting application which works fine on LAN.
But the problem is with connecting two PCs Manually.
Let's suppose there are two PCs (Application instances), PC A and PC B.
To make a connection I have to put the IP Address of the PC A into PC B and IP Address of the PC B into PC A.
I want to make the small code change where if the application is running on two PCs and they both are connected via LAN then both sides get the IP address automatically. Like auto-detection.
So my logic was to first know the IP Address on the LAN using arp -a command then writes the output in a text file and only obtain the IP addresses that start with 192... and check the instance of the application on each address using This Solution.
Unfortunately, I end up getting an error which states that.
Unhandled Exception: System.Runtime.InteropServices.COMException: The
RPC server is unavailable.
at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32
errorCode, IntPtr errorInfo) at
System.Management.ManagementScope.InitializeGuts(Object o) at
System.Management.ManagementScope.Initialize() at
System.Management.ManagementObjectSearcher.Initialize() at
System.Management.ManagementObjectSearcher.Get()
This is the code which i used to accomplish this task.
//Write the cmd Output in a text file
string strCmdText;
strCmdText = #"/K arp -a > C:\Test\Result.txt";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
//To get the IP address which starts with the 192.
const string ipPattern = #"^\s*(192\.168\.\d{1,3}\.\d{1,3}\b)";
var ipRegex = new Regex(ipPattern);
var ipAddresses192168 = File.ReadAllLines(#"C:\Test\Result.txt")
.Skip(3) // Skip 3 lines
.Where(line => ipRegex.IsMatch(line))
.Select(line => ipRegex.Match(line).Groups[1].Value);
foreach (var ipAddress in ipAddresses192168)
{
Console.WriteLine(ipAddress);
// Check for the running instance of the application on LAN network.
ManagementScope scope = new ManagementScope(#"\\" + ipAddress + #"\root\cimv2");
string query = "SELECT * FROM Win32_Process WHERE Name='WavPlayer.exe'";
var searcher = new ManagementObjectSearcher(query);
searcher.Scope = scope;
bool isRunning = searcher.Get().Count > 0;
}
So my question is, Is there any other straight forward process to accomplish this task using C#.net?
I am available to provide more info about this question so any help is appreciated.

Getting al ip's

So, i'm new to C# and visual studio and stuff. But i searched arround for how to get all ip's, Like you see in the Windows Firewall app. But I can't find any thing to get started with. I saw alot of posts about Dns, And stuff So i got this:
private void ip()
{
StringBuilder sb = new StringBuilder();
// Get host name
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Enumerate IP addresses
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
sb.AppendLine(strHostName + " - " + ipaddress);
label1.Text = sb.ToString().Replace(Environment.NewLine, "\n");
}
}
But I only get 4 ip's but i want all ip's currently connecting to my pc.
(And an i know there are alot of posts about this, but i'm searching for an answer for an beginning programmer to understand)
Could anybody help me?
Thanks!
That code you show gets the IPs assigned to the current machine.
What it sounds like you want is the equivalent of running netstat on the command line (get all active connections). This question should get you started: How to determine tcp port used by Windows process in C#
Basically you use GetActiveTcpConnections.

Checking if IPv6 is enabled on Windows 7 using 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;
}
}

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