Find permanently installed physical drives using WMI - windows

I'm using WMI to find all the Win32_DiskDrives on a machine. I want to exclude drives temporarily installed, like USB drives.
Is there a way to do this?

You can search for USB devices using WMI like this:
public void CollectUSBDevices()
{
NameValueCollection collection = new NameValueCollection();
ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("SELECT * FROM win32_pnpentity where deviceid like 'USB%'");
// Iterate through all found USB objects.
foreach (ManagementObject dm in searcher2.Get())
{
string nameValue = dm["Name"].ToString();
string devid = dm["DeviceID"].ToString();
if (nameValue.Contains("Generic USB Hub") || nameValue.Contains("USB Root Hub"))
continue;
if (nameValue.Contains("USB Mass Storage Device") || devid.Contains("USBSTOR\\"))
collection.Add("USBDevice", nameValue);
}
}

Related

Where is the list of device driver images stored in ETW?

I am trying to programatically get the list of device drives from an ETW with the great TraceProcessing Library which is used by WPA.
using ITraceProcessor processor = TraceProcessor.Create(myEtlFile, new
TraceProcessorSettings
{
AllowLostEvents = true,
AllowTimeInversion = true,
});
myProcesses = processor.UseProcesses();
foreach (var process in myProcesses.Result.Processes)
{
foreach (var dll in process.Images)
{
// get dll.Path, dll.FileVersion, dll.ProductVersion, dll.ProductName, dll.FileVersionNumber, dll.FileDescription
}
}
This works for every process except the Kernel (System(4)). Why do I have only 3 dlls in the System process? I would expect the driver files in the System process there as well. In CPU sampling the image is there so it looks like everything is right there.
This would be very useful to check for driver versions if the data is present. But so far I was not able to find it. Am I missing something here?
Happy to hear you enjoy using the TraceProcessor library!
Device drivers are logged against the "Idle (0)" process by ETW, here is an example:
using var tp = TraceProcessor.Create(#"trace.etl");
var processes = tp.UseProcesses();
tp.Process();
var idleProcess = processes.Result.Processes.FirstOrDefault(x => x.Id == 0);
foreach (var image in idleProcess?.Images)
{
Console.WriteLine(image.Path);
}

Trouble with connecting to wifi in code in Xamarin

I've been trying to connect to a specific wifi through code, but with no succcess.
This is what i've come up with:
public void ConnectToWifi(string ssid, string password)
{
WifiManager wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
if (!wifiManager.IsWifiEnabled)
{
wifiManager.SetWifiEnabled(true);
}
string formattedSsid = $"\"{ssid}\"";
string formattedPassword = $"\"{password}\"";
WifiConfiguration wifiConfig = new WifiConfiguration
{
Ssid = formattedSsid,
PreSharedKey = formattedPassword
};
var addNetwork = wifiManager.AddNetwork(wifiConfig);
WifiConfiguration network = wifiManager.ConfiguredNetworks.FirstOrDefault(n => n.Ssid == ssid);
if (network == null)
{
Console.WriteLine($"Cannot connect to network: {ssid}");
return;
}
wifiManager.Disconnect();
bool enableNetwork = wifiManager.EnableNetwork(network.NetworkId, true);
}
I've added permissions.
When testing it does turn the wifi on atleast, so i know it works until that point. What seems not to be working is the AddNetwork part.
I appreciate any help i can get!
You are missing one key method - reconnect(). You can read more about it in the WifiManager's docs here
The important part of the documentation is:
Reconnect to the currently active access point, if we are currently disconnected.
So, what you need to do it after you have disconnected and enabled your new network, call in the end this and you will be good to go:
wifiManager.Disconnect();
wifiManager.EnableNetwork(network.NetworkId, true);
wifiManager.Reconnect(); // This is the missing method
NB: Keep in mind that most of the WifiManager's code that you are using is being obsolete starting Android 10. So, if you want to target Android 10, then you will need to write an additional code for the connectivity for devices with Android 10+.

Determine GPU manufacturer on Windows

I'm going to write a Windows application and it would be useful if the app could tell what graphics card is being used. At the least, it would be helpful to see the manufacturer of the GPU. I'm not set on a programming language as of yet.
What windows library exposes this information?
See here for a C# way of doing it, using WMI. You could access WMI through pretty much any language:
C# detect which graphics card drives video
ManagementObjectSearcher searcher
= new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration");
string graphicsCard = string.Empty;
foreach (ManagementObject mo in searcher.Get())
{
foreach (PropertyData property in mo.Properties)
{
if (property.Name == "Description")
{
graphicsCard = property.Value.ToString();
}
}
}

Retrieve manufacturer information from device that AIR app is running on

Does anybody know of a way to retrieve information about the manufacturer/model of the device that the AIR app is running on. The Capabilities class doesn't seem to cut it.
The solution only needs to work for AIR apps running on Windows desktops or laptops, and it needn't be a descriptive string of the model - as long as it is a piece of data unique to a specific model or device (or at least the specific manufacturer).
On Windows, it's possible to query the motherboard's serial number with WMIC, or Windows Management Instrumentation Command-line. Therefore, you can simply pass the command wmic baseboard get serialnumber as an argument to cmd.exe using flash.desktop.NativeProcess without the need for a Native Extension.
Since the AIR NativeProcess API is being used, you must use the Extended Desktop application profile and package your application with a native installer.
package
{
//Imports
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
import flash.events.ProgressEvent;
import flash.filesystem.File;
//Class
[SWF(width = "600", height = "250", frameRate = "60", backgroundColor = "0x000000")]
public class Main extends Sprite
{
//Constants
private static const MOTHERBOARD_SERIALNUMBER_COMMAND:String = "wmic baseboard get serialnumber";
//Properties
private var nativeProcess:NativeProcess;
//Constructor
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
init();
}
//Init
private function init():void
{
if (!NativeProcess.isSupported)
{
throw new Error("Native Process is not supported.");
}
var file:File = new File("C:\\Windows\\System32\\cmd.exe");
var args:Vector.<String> = new Vector.<String>();
args.push("/c");
args.push(MOTHERBOARD_SERIALNUMBER_COMMAND);
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = file;
nativeProcessStartupInfo.arguments = args;
nativeProcess = new NativeProcess();
nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, outputDataEventHandler);
nativeProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, outputErrorEventHandler);
nativeProcess.start(nativeProcessStartupInfo);
}
//Output Data Event Handler
private function outputDataEventHandler(event:ProgressEvent):void
{
var output:String = nativeProcess.standardOutput.readUTFBytes(nativeProcess.standardOutput.bytesAvailable);
nativeProcess.exit();
trace(output);
}
//Output Error Event Handler
private function outputErrorEventHandler(event:ProgressEvent):void
{
nativeProcess.exit();
throw new Error(event);
}
}
}
[EDIT]
Alternatively, if you would also like to retreive the motherboard's manufacturer, model number and serial number, you can update the string constant to this:
//Constants
private static const MOTHERBOARD_INFO:String = "wmic baseboard get product, manufacturer, serialnumber";
[EDIT 2]
I just learned that the following WMIC command will return the name, vendor and identifying number of a machine. It sounds exactly what your looking for:
//Constants
private static const CSPRODUCT_INFO:String = "wmic csproduct get name, vendor, identifyingNumber";
However, keep in mind that for custom built PCs, such as my own, this command returns nothing. Well, not exactly nothing, but instead of something typical like:
IdentifyingNumber Name Vendor
99L9891 Latitude D610 Dell Inc.
My custom build returns this:
IdentifyingNumber Name Vendor
System Serial Number System Product Name System manufacturer

Getting current IP of windows phone 7

I am building a windows phone 7.1 app. I need to find out if the phone is connected to any Wifi and if so, what is its current IP in the local network (ie. 192.168.0.100 like this).
I've been trying to find out these information for some time now. Please help.
I've managed to get the local IP on my console app by using the following code
public void ScanIP()
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
String localIP = ip.ToString();
Console.WriteLine(localIP);
}
}
Console.ReadKey();
}
However, I need similar thing done for windows mobile 7 app. Any idea ? Please share.
Do a multicast and listen for replies. Once you identify your multicast message, you can get the IP of the sender (which is yourself). You can use UdpAnySourceMulticastClient to do the multicasting. In case you're not in a wifi network, you will get a socket failure in the EndJoinGroup call. You should handle the exception and pass a specific value indicating you're not in a wifi network.
More info in this blog post by Andy Pennell.
it will provide you the ip address of the phone ...
public static IPAddress Find()
{
List<string> ipAddresses = new List<string>();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
if (hn.IPInformation != null)
{
string ipAddress = hn.DisplayName;
ipAddresses.Add(ipAddress);
}
}
IPAddress address = IPAddress.Parse(ipAddresses[0]);
return address;
}

Resources