ManagementObjectSearcher to detect drivers for Sixnet - c#

This is what I get for installed software. I downloaded the Sixnet driver package and installed it. Now I want to detect if it has actually installed the drivers with the code below:
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPSignedDriver");
foreach (ManagementObject obj in searcher.Get())
{
//Looking for Sixnet USB Ethernet/RNDIS Gadget. Only try on objects that actually have a description.
if (obj.GetPropertyValue("Description") != null)
{
try
{
if (obj.GetPropertyValue("Description").ToString().Contains("Sixnet USB Ethernet"))
{
return true;
}
}
catch (Exception e)
{
}
}
}
return false;
I am pretty sure it was returning true earlier today but now I cannot get this block to work, it does not detect the driver.
Lower down in the code I connect to the NIC and that works but it was my understanding that it would only connect to the NIC if the driver was actually installed because the default driver does not work.
Below is my network connections and I can see the modem as Network 5 - the one with Sixnet in the name. Why does my code not want to show that the drivers are installed? I want to run the software to first check if the drivers were installed and then if I can connect to the Sixnet modem via a USB cable. Thank you
Edit: Ok, I changed obj.GetPropertyValue("Description") to obj.GetPropertyValue("Devicename") and it did not work immediately so I moved on to other work and 10 minutes later it worked? How do I refresh the WMI database?
Edit #2:
private bool Check_Drivers()
{
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPSignedDriver where DeviceName LIKE '%Sixnet USB%'");
if (searcher.Get().Count > 0)
{
searcher = null;
return true;
}
searcher = null;
return false;
}
I changed my whole method to the code above. The searcher = null; was my attempt to make sure every call to this method reloads the management object.

Related

Microsoft OPOS hangs on Claim after PC restart

I am using 'Microsoft Point of Service for .NET v1.14.' dlls to connect with Posiflex Thermal Printer (PP8800 model).
Code snippet which I am using is also pasted below. I am running this code from WPF application.
But I am facing one strange issue. If printer is powered on and machine on which the printer is connected is restarted, below pasted code is not able to claim the printer and it also does not come out of the Claim() statement. Whereas if I power off the printer and then power it on before running the below code snippet then it is able to claim the printer.
PosExplorer posExplorer = new PosExplorer();
var printerList = posExplorer.GetDevices(DeviceType.PosPrinter);
foreach (DeviceInfo item in printerList)
{
if (item.ServiceObjectName != printerName)
continue;
posPrinter = (PosPrinter)posExplorer.CreateInstance(item);
posPrinter.Open();
posPrinter.PowerNotify = PowerNotification.Enabled;
posPrinter.FlagWhenIdle = true;
posPrinter.StatusUpdateEvent += PosPrinter_StatusUpdateEvent;
posPrinter.DirectIOEvent += PosPrinter_DirectIOEvent;
posPrinter.ErrorEvent += PosPrinter_ErrorEvent;
posPrinter.OutputCompleteEvent += PosPrinter_OutputCompleteEvent;
try
{
posPrinter.Claim(100);
if (posPrinter.Claimed)
{
isPrinterClaimed = true;
}
else
{
isPrinterClaimed = false;
}
}
catch (Exception ex)
{
isPrinterClaimed = false;
}
break;
}
Has anybody faced the same issue while using OPOS libraries?

C# Update Network Adapter Name partially working (WinReg good, ipconfig bad)

Given the following function:
private void UpdateNetworkAdapterName(string pnpDevID, string oldAdpterName, string newAdapterName)
{
string guid = "";
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
if (string.Equals(m["PNPDeviceID"].ToString(), pnpDevID))
{
guid = m["GUID"].ToString();
break;
}
}
RegistryKey regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Environment.MachineName, RegistryView.Registry64);
regKey = regKey.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + guid + "\\Connection", true);
regKey.SetValue("Name", newAdapterName);
bool successful = false;
foreach (NetworkInterface netAd in NetworkInterface.GetAllNetworkInterfaces())
{
if (netAd.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
if (string.Equals(netAd.Name, newAdapterName))
{
Console.WriteLine($"Successfully updated network adapter from {oldAdpterName} to {newAdapterName}");
successful = true;
break;
}
}
}
if (!successful)
{
Console.WriteLine($"Failed to updated network adapter from {oldAdpterName} to {newAdapterName}");
}
}
This successfully updated the correct adapter 'Name' data within Windows Registry and the correct adapter name in the Network and Sharing Centre.
However, I get the failed message internally from the code (no exceptions thrown though (have removed exception handling code for readability)) and doing an ipconfig shows that the adpater name has not been updated.
Environment is Windows10 (needs to also work on Windows7), both 64bit architectures, application built as a 32bit application.
Any ideas what is going on? I am at a total loss at this point.
Thanks in advance.
Just realised that I have all the information to just do a netsh command:
netsh interface set interface name="" newname=""

C# Application: Get Network Shares On Another Computer

First of all, apologies if this doesn't make sense, and/or it has already been asked (although searching didn't find anything).
I have an application which sets default printers for our end users but I would like to expand it by also making it able to install printers from a remote machine as well.
What I need to do is on Form_Load populate a Combo Box with all network shares from the print server.
I am shooting in the dark and am wondering if anyone can shed some light.
I believe this works.
This isnt my code originally but I cant remember where it came from.
using System.Management;
private void btnGetPrinters_Click(object sender, EventArgs e) {
// Use the ObjectQuery to get the list of configured printers.
ObjectQuery oquery = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mosearcher = new ManagementObjectSearcher(oquery);
ManagementObjectCollection moc = mosearcher.Get();
foreach (ManagementObject mo in moc)
{
PropertyDataCollection pdc = mo.Properties;
foreach (System.Management.PropertyData pd in pdc)
{
if ((bool)mo["Network"])
{
cmbPrinters.Items.Add(mo[pd.Name]);
}
}
}
}

Determine if Kinect is plugged in using Microsoft SDK or ManagementObjectSearcher

I'm trying to determine if my Kinect is plugged into the PC using the ManagementObjectSearcher. I'm not sure what to query because it is NOT listed as a USB Device. Instead it is listed as a "Microsoft Kinect" hardware device.
Below is what I'm going fussing with:
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(#"Select * from Win32_SOMETYPE"))
{
foreach (ManagementObject managementObject in searcher.Get())
{
foreach (PropertyData propertyData in managementObject.Properties)
{
}
}
}
If you are using the latest Windows SDK, here is some example code to get available Kinect connections:
switch (Runtime.Kinects.Count)
{
case 0:
//Do something if none.
break;
default:
//Do something if present.
break;
}
Good Luck!
Matt

Detect Server Display Resolution

On windows server 2008 can I have a web service or something I can query from a C# application as to the display properties (resolution (height & width)). The C# application does not run on the server so I cannot just detect it from the application itself.
Addition to help explain why:
I will have a user named "display" and that will be logged on displaying a website (on the server) and I want to be able to check the display from the desktop application so the user knows what resolution to design a template for. The resolution will change from different displays so it can't be a set value.
I'd recommend just querying the server using WMI. Check the third example here:
http://msdn.microsoft.com/en-us/library/aa394591%28v=vs.85%29.aspx
My Code
This is the code that I used to solve the problem:
System.Management.ConnectionOptions oConnectionOptions = new System.Management.ConnectionOptions();
{
oConnectionOptions.Username = ServerManagement.GetServerUser();
oConnectionOptions.Password = ServerManagement.GetServerPassword();
}
ManagementPath oPath = new ManagementPath("\\\\" + ServerManagement.GetServerAddress() + "\\root\\cimv2");
ManagementScope oScope = new ManagementScope(oPath, oConnectionOptions);
try
{
oScope.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");
ManagementObjectCollection obj = searcher.Get();
foreach (ManagementObject service in obj)
{
this.DisplayHeight = Convert.ToInt16(service["ScreenHeight"]);
this.DisplayWidth = Convert.ToInt16(service["ScreenWidth"]);
}
}
catch (Exception)
{
MessageBox.Show("Cannot connect to server, please try again later.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

Categories