How should I using C# to read partition table/boot sector? - c#

Hi i'v look through the WMI class... but none can help me in getting the partition table information... now i'm having a project which is to display the USB thumbdrive's .MBR how should i go about it? really puzzle... Any help and guide will be very much appreciated!
p.s. the code can only be written in C#
Edit
Thank you! I've browse through the CreateFile documentation... Still wondering how should I use P/Invoke to call CreateFile and to read the boot sector( display out the .MBR )? Do you have any reference code for this portion? Thank you once again!!

If you want you can use WMI to get information about any drive. To do this you need to query the corresponding WMI classes. With C# you must add these references:
System.Management
and using statements:
using System.Management;
To get disk info for which are attached to your computer you can use this query:
Select * From Win32_DiskDrive
with C#, you can query like:
SelectQuery wmi_sorgusu = new SelectQuery("Select * from Win32_DiskDrive");
ManagementObjectSearcher wmi_bulucu = new ManagementObjectSearcher( wmi_sorgusu );
foreach (ManagementObject wmi_nesne in wmi_bulucu.Get()) {
Console.WriteLine(wmi_nesne.GetPropertyValue( "DeviceID" ).ToString());
Console.WriteLine(wmi_nesne.GetPropertyValue( "InterfaceType" ).ToString());
Console.WriteLine(wmi_nesne.GetPropertyValue( "Caption" ).ToString());
Console.WriteLine(wmi_nesne.GetPropertyValue( "Status" ).ToString());
Console.WriteLine(wmi_nesne.GetPropertyValue( "MediaLoaded" ).ToString());
//... etc
}
After getting the device parameters, you can use the same method to query Win32_DiskPartition WMI Class. You can give the device parameters to SELECT query as WHERE clause. Queries to Win32_DiskPartition returns all the partitions of all drives attached to system.

If you're prepared to parse the raw information yourself, you can use P/Invoke to call CreateFile. If your user account has enough mojo, you can open the raw disk device by passing it a string, something like "\\.\PHYSICALDRIVE0". See the documentation for CreateFile for more details.

To be honest, I would be very much surprised if you can't open it with IO.FileStream with the same magic path.

Indeed C# FileStream cannot open physical device directly:
Unhandled Exception: System.ArgumentException: FileStream will not
open Win32 devices such as disk partitions and tape drives. Avoid use
of "\.\" in the path.

Related

delete user from zk attendance device using c# api

I have set of ZK attendance device that using static IP address. I'm enrolling users to this machines using zkmkeeper.dll in C#. now i want to delete old users from these machines using system. I can't find the function to delete users, but there is function to disable. I want to delete that users permanently from devices.
I had a similar issue when deleting userInfo or enrolled data but i found a workaround
objCZKEM.DeleteEnrollData(machineId, userId, machineId, 12)
according to the documentation :
12 indicates deleting the user (including the fingerprints, card number and password).
#Yuuuucef, thank you for reference. According to documentation
5.2.4.3 DeleteEnrollData
VARIANT_BOOL DeleteEnrollData(LONG dwMachineNumber, LONG dwEnrollNumber,
LONG dwEMachineNumber, LONG dwBackupNumber)
The values of dwMachineNumber and dwEMachineNumber must be the same
After trying many ways I have finally found a way. clear all user data using axCZKEM1.ClearData(Convert.ToInt32(number), flag) and re enroll the data using Database.
The way I found works for me.
Use the SSR_deleteEnrollData function with only 3 parameters (while notice asks for 4 parameters)
SSR_deleteEnrolData(dwMachineNumber , dwEnrollNumber , dwBackupNumber)
Maybe depends of device version.

SMBIOS - Get SPD (Serial Presence Detect) Modules Information C#

I searched a lot but did not find any working codes getting SPD tables information via C#. Out there there are lots of softwares which get this info but HOW?
as shown in the image, for RAM devices, you can see Manufacture's name which can not be retrieve at all by WMI etc
If there is a DLL for using in C# will be perfect also
After some Research found this:
https://github.com/sapozhnikovay/SMBIOS
but it can not read table 17 to get memory device information.
Once I was researching about this, you need to get this information through SMBUS (not SMBIOS). But you need to create a driver (WDM in C/C++) to access this information.
Make sure you have added System.Management as a reference.
Here is a string that will return almost any information you want from the component :
private string getComponent(string hwClass, string syntax)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwClass);
foreach (ManagementObject mj in mos.Get())
{
return Convert.ToString(mj[syntax]);
}
return null;
}
Using the string would look like this, say on a button click :
label1.Text = getComponent("Win32_PhysicalMemory", "SerialNumber");
I tested it and it returned a serial number, you can also look at the list of things you can put in like manufacturer, name, capacity etc.
I got all of this information from this YouTube video.
You can find all of the devices and their properties here (CPU, GPU, etc.)

Retrieve formatted performance data from WMI

I'm writing a WMI Provider and I've managed to retrieve all info on the Computer System and Hardware Classes but cannot get data I want from the Performance Counter Classes. (Win32 Classes)
Looking through the MSDN documentation and using examples they provide, I've come up with a shell script that should return all properties of the Win32_PerfFormattedData abstract base class.
Script:
$osClass = New-Object System.Management.ManagementClass Win32_PerfFormattedData
$osClass.Options.UseAmendedQualifiers = $true
# Get the Properties in the class
$properties = $osClass.Properties
"This class has {0} properties as follows:" -f $properties.count
# display the Property name, description, type, qualifiers and instance values
foreach ($property in $properties) {
"Property Name: {0}" -f $property.Name
"Description: {0}" -f $($property.Qualifiers["Description"].Value)
"Type: {0}" -f $property.Type
"-------"
}
(referenced from here)
The problem is that I'm only retrieving the properties from it's base class Win32_Perf
EDIT
After doing more research, I found this on MSDN:
The WMI formatted class name for a counter object is of the form "Win32_PerfFormattedData_service_name_object_name"
I am trying to get the service_name's that are in the Win32_PerfFormattedData and the object_name's within those services.
I'm unsure whether getting the properties is how I want to go about this now but I cannot find any documentation to get the services. Are they the same thing? And, if not, how can I get the info I require? (service_name's & object_name's)
I've also tried this in some C# code and get the same result:
ManagementClass processClass = new ManagementClass();
processClass.Path = new ManagementPath("Win32_PerfFormattedData");
PropertyDataCollection properties = processClass.Properties;
Console.WriteLine("\nProperties:");
foreach (PropertyData property in properties)
{
Console.WriteLine(property.Name);
}
And I tried retrieving the methods to check if that is what I wanted but nothing is returned:
Console.WriteLine("Methods: ");
foreach (MethodData method in methods)
{
Console.WriteLine(method.Name);
}
EDIT 2
Is there another way to retrieve this data? I've looked all through the MSDN documentation on the WMI and I think to get the information I want, I have to access that class (Win32_PerfFormattedData). Sorts of values I want to retrieve:
CPU
RAM
Drives (SSD/HDD)
Processes
OS
GPU
I've retrieved a few classes that will give basic information about some of these but will not provide up to date data on, for example, the temperature of each logical processor. I've managed to get 1 service from the class Win32_PerfFormattedData_PerfOS_Processor which provides the load % of each logical processor but that class must holds other services which I need.
Win32_PerfFormattedData_* classes are located under the "root\cimv2" namespace. To enumerate these classes (and get the service names) you run the following WQL query against that namespace:
SELECT * FROM meta_class WHERE __Class LIKE "Win32_PerfFormattedData%"
Actually you can omit the namespace (at least with ManagementObjectSearcher) in which case the search occurs everywhere. Here is how to search through WMI with C#:
void SearchWmi()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM meta_class WHERE __Class LIKE \"Win32_PerfFormattedData%\"");
foreach (ManagementClass wmiClass in searcher.Get())
{
Console.WriteLine(wmiClass["__CLASS"].ToString());
}
}
You need to add referece to System.Management as well as the corresponding using directive.
You could find performance data about the:
CPU: Win32_PerfFormattedData_Processor
RAM: Win32_PerfFormattedData_Memory
OS: Win32_PerfFormattedData_System
Drives: Win32_PerfFormattedData_PerfDisk_*
Processes: Win32_PerfFormattedData_PerfProc_*
I have no idea about the GPU. Most likely it is driver dependent.
There are numerous WMI explorer tools out there with a UI and all the good stuff. Have you tried some? I use the "WMI Explorer 2.0"
You can download it from here

Where do I find the name displayed in "Devices and Printers"

I have a serial device connected via Bluetooth. It shows up nicely on COM4. I can communicate with it without a problem.
I want to make it simpler for the user to locate (ideally, I'll auto-detect it), so I want to find it by name. In the "Devices and Printers" list, I get a valid name, which is perfect. However, I can't seem to find that value programatically. I've tried a ton of stuff using the "ManagementObjectSearcher" class, including listing out all the Properties and SystemProperties, but no values match the name displayed in "Devices and Printers".
If I look in the "Device Manager" list, it just shows "Standard Serial over Bluetooth link (COM4)", which is not useful for identifying it, obviously.
So how the heck to I get the displayed name in the "Devices and Printers" list?
is this what you're looking for?
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings.installedprinters(v=vs.110).aspx
So, I found a solution. I grabbed the library from these guys:
http://32feet.codeplex.com/
Using that library, added these 2 lines:
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
That gave me the device "DeviceName" (the name I was after) and "DeviceAddress" (a chunk of the device id, basically).
I then queried the system using the "ManagementObjectSearcher", which gave me a list of COM ports and device IDs (System.Management namespace).
ConnectionOptions options = ProcessConnection.ProcessConnectionOptions();
ManagementScope connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, #"\root\CIMV2");
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity");
ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);
...etc as I looped over the results, pulled out the COM ports, and so on
I mapped the device IDs from the "ManagementObject" values back to the "devices" list, merged the results, and ended up with something that had the name, device id, a flag indicating if it was a bluetooth device, and the "human readable" name from the bluetooth device, if it existed.
Painful, but it works fairly well. It's slow (client.DiscoverDevices() takes awhile), but that's survivable in my case.

How to get serial number of USB-Stick in C#

How do I get the internal serial number of a USB-Stick or USB-HardDrive in C#?
Try this:
// add a reference to the System.Management assembly and
// import the System.Management namespace at the top in your "using" statement.
// Then in a method, or on a button click:
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
foreach (ManagementObject currentObject in theSearcher.Get())
{
ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");
MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString());
}
Source: http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/f4447ed3-7e5f-4635-a28a-afff0b620156/
A solution using Win32 is described here
Edit: the original link seems to have gone missing. The above is a cached copy, and the author also wrote some sample code in VB.Net which is still online here.
I had problems with the solution offered by Yuval Adam as every USB stick I tried return blank on windows 7.
I solved this by just looking at the property PNPDeviceId on the current object.
E.g.
currentObject["PNPDeviceID"].ToString();
Not sure how valid this is but it worked for me on the 3 USB Sticks I tried

Categories

Resources