How to get multiple network interface cards data - c#

I have code, which find my NIC card IP address, IP subnet, Gateway, Mac and network card name (description). But the problem is, I have multiple NIC cards and WiFi on my PC. And this program instead of 4 NIC cards shows me only one primary.
How can solve this problem ?
public void NIC_data()
{
ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
string[] defaultgateways = (string[])mo["DefaultIPGateway"];
textBox1.Text = string.Format("Network Card: {0}", mo["Description"]);
textBox2.Text = string.Format(" MAC Address: {0}", mo["MACAddress"]);
foreach (string ipaddress in addresses)
{
textBox3.Text = string.Format(" IP Address: {0}", ipaddress);
}
foreach (string subnet in subnets)
{
textBox4.Text = string.Format(" Subnet Mask: {0}", subnet);
}
foreach (string defaultgateway in defaultgateways)
{
textBox5.Text = string.Format(" Gateway: {0}", defaultgateway);
}
}

You just assign the last value of the loop to textBoxes like textBox3.Text = .....
either append to textBox3.Text += ... or use a combo box.

Related

How can I get a network's connection name in C#?

I want to judge if the computer running my program is in a certain network.
I have tried the code below
ManagementClass vNetworkAdapter = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection vNetworkAdapters = vNetworkAdapter.GetInstances();
foreach (ManagementObject vNetworkAdapterInfo in vNetworkAdapters)
{
string ID = (string)vNetworkAdapterInfo.Properties["NetConnectionID"].Value;
string Caption = (string)vNetworkAdapterInfo.Properties["Caption"].Value;
string Description = (string)vNetworkAdapterInfo.Properties["Description"].Value;
string SSID = (string)vNetworkAdapterInfo.Properties["DeviceID"].Value;
if (ID != null)
{
if (ID == "以太网")//judge certain type of connection by name, I only considered the wired and wireless connection
{
Console.WriteLine("以太网" + "\n" + Caption + "\n" + Description);
Console.WriteLine(SSID);
}
else if (ID == "WLAN")
{
Console.WriteLine("WLAN" + "\n" + Caption + "\n" + Description);
Console.WriteLine(SSID);
}
}
Console.WriteLine("");
}
It returns With
WLAN
[00000002] Killer(R) Wi-Fi 6 AX1650i 160MHz Wireless Network Adapter (201NGW)
Killer(R) Wi-Fi 6 AX1650i 160MHz Wireless Network Adapter (201NGW)
以太网
[00000003] Killer E2500 Gigabit Ethernet Controller
Killer E2500 Gigabit Ethernet Controller
But I not wanting this name, I want to get the name shown when we are connecting to a certain network(known as SSID for wireless connection), What I should do? thanks a lot!

How to use WMI to get BOTH the numerical vid/pid and the drive letter from an usb drive

I am trying to show the vid/pid and the drive letter of a usb drive when I connect it to my computer. I'm actually able to do both, but not at the same time.
1: I can use Win32_DiskDrive to get to Win32_LogicalDisk and extract the drive letter. But the deviceId has a non-numerical pid/vid which I can't use.
2: I can use Win32_USBHub or Win32_PnPEntity to extract the vid/pid, but I can't find a link between them and the drive letter.
public void CheckForUsbDevice()
{
string text =
"SELECT * FROM __InstanceCreationEvent " +
"WITHIN 2 "
+ "WHERE TargetInstance ISA 'Win32_PnPEntity'";
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery(text);
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Query = query;
watcher.Start();
Console.ReadKey();
}
static readonly Guid GUID_DEVCLASS_USB = new Guid("{36fc9e60-c465-11cf-8056-444553540000}");
static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
if ((new Guid((string)instance["ClassGuid"]) == GUID_DEVCLASS_USB) && ((string)instance.Properties["Name"].Value == "USB Mass Storage Device"))
{
// we're only interested by USB devices, dump all props
foreach (var property in instance.Properties)
{
Console.WriteLine(property.Name + " = " + property.Value);
}
}
}
This has the output:
Availability =
Caption = USB Mass Storage Device
ClassGuid = {36fc9e60-c465-11cf-8056-444553540000}
CompatibleID = System.String[]
ConfigManagerErrorCode = 0
ConfigManagerUserConfig = False
CreationClassName = Win32_PnPEntity
Description = USB Mass Storage Device
DeviceID = USB\VID_0911&PID_1F40&MI_00\6&27CAD51B&0&0000
ErrorCleared =
ErrorDescription =
HardwareID = System.String[]
InstallDate =
LastErrorCode =
Manufacturer = Compatible USB storage device
Name = USB Mass Storage Device
PNPClass = USB
PNPDeviceID = USB\VID_0911&PID_1F40&MI_00\6&27CAD51B&0&0000
PowerManagementCapabilities =
PowerManagementSupported =
Present = True
Service = USBSTOR
Status = OK
StatusInfo =
SystemCreationClassName = Win32_ComputerSystem
SystemName = WINDEV1905EVAL
I need a link between WMI classed containing the numerical pid/vid and the drive letter or a different way how that can be achieved.
I think it might be possible to use DeviceIoControl, but I have no idea where to start there.
Any help is highly appreciated.

ManagmentObject queries for windows (C#) to find usb device

I'm trying to find a particular USB device (1 or more) connected to my computer and retrieve the relevant path to the mounted drive. Ideally, it would be by finding the VID/PID of the USB device, but I'm not sure how to do that yet. The following works, but there must be some way to get the data in a single query.
What I'm doing here is looking or a physical drive that has a model matching HS SD Card Bridge USB Device and finding the physical drive # associated and using that to find the mounted partition..
foreach (ManagementObject disk in disks.Get()) {
//look for drives that match our string
Match m = Regex.Match(disk["model"].ToString(), "HS SD Card Bridge USB Device");
if (m.Success) {
m = Regex.Match(disk["DeviceID"].ToString(), #"PHYSICALDRIVE(\d+)");
if (m.Success) {
int driveNumber = Int32.Parse(m.Groups[1].ToString());
ManagementObjectSearcher mapping = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
foreach (ManagementObject map in mapping.Get()) {
m = Regex.Match(map["Antecedent"].ToString(), #"Disk #" + driveNumber + ",");
if (m.Success) {
string drive = map["Dependent"].ToString();
m = Regex.Match(drive, #"([A-Z]):");
if (m.Success) {
drive = m.Groups[1].ToString(); //< -- **FOUND**
}
}
}
//USBDevice dev = new USBDevice("", "");
// list.Items.Add();
Console.WriteLine("");
}
}
}
is there a way to do this from the VID/PID and a way to construct the search query so it requires just one query?
This is the one I used earlier . This will not be the answer. But will help you .
public int GetAvailableDisks()
{
int deviceFound = 0;
try
{
// browse all USB WMI physical disks
foreach (ManagementObject drive in
new ManagementObjectSearcher(
"select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get())
{
ManagementObject partition = new ManagementObjectSearcher(String.Format(
"associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
drive["DeviceID"])).First();
if (partition == null) continue;
// associate partitions with logical disks (drive letter volumes)
ManagementObject logical = new ManagementObjectSearcher(String.Format(
"associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
partition["DeviceID"])).First();
if (logical != null)
{
// finally find the logical disk entry to determine the volume name - Not necesssary
//ManagementObject volume = new ManagementObjectSearcher(String.Format(
// "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'",
// logical["Name"])).First();
string temp = logical["Name"].ToString() + "\\";
// +" " + volume["VolumeName"].ToString(); Future purpose if Device Name required
deviceFound++;
if (deviceFound > 1)
{
MessageBox.Show(#"Multiple Removeable media found. Please remove the another device");
deviceFound--;
}
else
{
driveName = temp;
}
}
}
}
catch (Exception diskEnumerateException)
{
}
return deviceFound;
}

How can I check for available disk space?

I need a way to check available disk space on a remote Windows server before copying files to that server. Using this method I can check to see if the primary server is full and if it is, then I'll copy the files to a secondary server.
How can I check for available disk space using C#/ASP.net 2.0?
You can check it by doing the following:
Add the System.Management.dll as a reference to your project.
Use the following code to get the diskspace:
using System;
using System.Management;
public string GetFreeSpace();
{
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
string freespace = disk["FreeSpace"];
return freespace;
}
There are a myriad of ways to do it, I'd check the System.Management namespace for more ways.
Here's one such way from that page:
public void GetDiskspace()
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\localhost\\root\\cimv2",
options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
ManagementObjectCollection queryCollection1 = searcher1.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Computer Name : {0}", m["csname"]);
Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}", m["Caption"]);
Console.WriteLine("Version: {0}", m["Version"]);
Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
Console.WriteLine();
}
foreach (ManagementObject mo in queryCollection1)
{
// Display Logical Disks information
Console.WriteLine(" Disk Name : {0}", mo["Name"]);
Console.WriteLine(" Disk Size : {0}", mo["Size"]);
Console.WriteLine(" FreeSpace : {0}", mo["FreeSpace"]);
Console.WriteLine(" Disk DeviceID : {0}", mo["DeviceID"]);
Console.WriteLine(" Disk VolumeName : {0}", mo["VolumeName"]);
Console.WriteLine(" Disk SystemName : {0}", mo["SystemName"]);
Console.WriteLine("Disk VolumeSerialNumber : {0}", mo["VolumeSerialNumber"]);
Console.WriteLine();
}
string line;
line = Console.ReadLine();
}
by using this code
static void Main()
{
try
{
DriveInfo driveInfo = new DriveInfo(#"C:");
long FreeSpace = driveInfo.AvailableFreeSpace;
}
catch (System.IO.IOException errorMesage)
{
Console.WriteLine(errorMesage);
}
}
IF you are getting the error 'The device is not ready' .i.e your device is not ready .
If you are trying this code for a CD drive without CD you will get the same error : )
This seems to be an option from the System.IO:
DriveInfo c = new DriveInfo("C");
long cAvailableSpace = c.AvailableFreeSpace;
You can use the DriveInfo class
DriveInfo[] oDrvs = DriveInfo.GetDrives();
foreach (var Drv in oDrvs) {
if (Drv.IsReady) {
Console.WriteLine(Drv.Name + " " + Drv.AvailableFreeSpace.ToString);
}
}
You can use WMI, see this related question:
Best way to query disk space on remote server

Determine the IP Address of a Printer in C#

I would like to determine the IP address of a printer, using C# (.NET 2.0). I have only the printer share name as set up on the Windows OS, in the format \\PC Name\Printer Name. The printer is a network printer, and has a different IP address to the PC. Does anyone have any pointers?
Thanks in advance for your help.
Regards, Andy.
Just adding an another solution here using .Net Framework 4.0 or higher
Using System.Printing
var server = new PrintServer();
var queues = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
foreach (var queue in queues)
{
string printerName = queue.Name;
string printerPort = queue.QueuePort.Name;
}
Check this question: How to get Printer Info in C#.NET?. I think that you have to get the property PortName from the WMI properties.
I know this is an old post, but I had the same issue where I was able to get the Printer Port name, but not the IP. In my case I couldn't rely on the Port Name being IP_[IP Address] but found how to get hold of the actual IP from the port name.
Windows stores the information about ports in the registry under
HKLM\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\[port name]
This key contains the values set up in the port configuration page, including IP address and port number.
A quick C# example to get the IP address
using Microsoft.Win32;
RegistryKey key = Registry.LocalMachine.OpenSubKey(#"System\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" + printerPortName, RegistryKeyPermissionCheck.Default, System.Security.AccessControl.RegistryRights.QueryValues);
if (key != null)
{
String IP = (String)key.GetValue("IPAddress", String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
}
Using WIN32_Printer class is not enough here. It should be combined with Win32_TCPIPPrinterPort class.
Below is the code which should help:
static void Main(string[] args)
{
var scope = new ManagementScope(#"\root\cimv2");
scope.Connect();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
var results = searcher.Get();
Console.WriteLine("Network printers list:");
foreach (var printer in results)
{
var portName = printer.Properties["PortName"].Value;
var searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
var results2 = searcher2.Get();
foreach (var printer2 in results2)
{
Console.WriteLine("Name:" + printer.Properties["Name"].Value);
//Console.WriteLine("PortName:" + portName);
Console.WriteLine("PortNumber:" + printer2.Properties["PortNumber"].Value);
Console.WriteLine("HostAddress:" + printer2.Properties["HostAddress"].Value);
}
Console.WriteLine();
}
Console.ReadLine();
}
string printerName = "POS-80C";
LocalPrintServer server = new LocalPrintServer();
PrintQueue printQueue = server.GetPrintQueue(printerName);
string portName = printQueue.QueuePort.Name;
string portNumber = "";
string hostAddress = "";
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
var results = searcher.Get();
foreach (var printer in results)
{
portNumber = (printer.Properties["PortNumber"].Value).ToString();
hostAddress = (printer.Properties["HostAddress"].Value).ToString();
}
Is this printer set up in a network which has Active Directory?
Or is this on your own local network with just a switch and your printer plugged into it?
If it is the former, then you should be able to query for it based on the "printer name". This article show how to get c# .net to connect to the AD. But this does require some knowledge of AD servers in your network.
This solution seems a bit long to me, but may be a good starting point?
Based on the link How to get Printer Info in .NET? (Thanks, Panos, I was already looking at the link!), I have the following solution from Panos's answer:
using System.Management;
...
string printerName = "YourPrinterName";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject printer in coll)
{
string portName = printer["PortName"].ToString();
if(portName.StartsWith("IP_"))
{
Console.WriteLine(string.Format("Printer IP Address: {0}", portName.Substring(3)));
}
}
Obviously, this only works if the port name for the printer is given in the format "IP_IPAddress", which is I believe is the default.

Categories