Where can I get info about notebook's model name? - c#

I trying to get info about HW from notebooks. I do this via WMI, but not always there contains the info about notebook model for example. It depends on the manufacturer, SONY never contains HW info in WMI.
FileStream fs = new FileStream(#"C:\log.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
string[] Names = { "Win32_Fan", "Win32_HeatPipe", "Win32_Refrigeration", "Win32_TemperatureProbe", "Win32_Keyboard", "Win32_PointingDevice",
"Win32_AutochkSetting", "Win32_CDROMDrive" , "Win32_DiskDrive" ,"Win32_FloppyDrive", "Win32_PhysicalMedia","Win32_TapeDrive" ,
"Win32_1394Controller","Win32_1394ControllerDevice", "Win32_AllocatedResource", "Win32_AssociatedProcessorMemory","Win32_BaseBoard",
"Win32_BIOS", "Win32_Bus","Win32_CacheMemory","Win32_ControllerHasHub","Win32_DeviceBus","Win32_DeviceMemoryAddress", "Win32_DeviceSettings",
"Win32_DMAChannel","Win32_FloppyController","Win32_IDEController","Win32_IDEControllerDevice","Win32_InfraredDevice","Win32_IRQResource","Win32_MemoryArray",
"Win32_MemoryArrayLocation","Win32_MemoryDevice","Win32_MemoryDeviceArray","Win32_MemoryDeviceLocation","Win32_MotherboardDevice","Win32_OnBoardDevice",
"Win32_ParallelPort","Win32_PCMCIAController","Win32_PhysicalMemory","Win32_PhysicalMemoryArray","Win32_PhysicalMemoryLocation","Win32_PNPAllocatedResource",
"Win32_PNPDevice","Win32_PNPEntity","Win32_PortConnector","Win32_PortResource","Win32_Processor","Win32_SCSIController", "Win32_SCSIControllerDevice","Win32_SerialPort",
"Win32_SerialPortConfiguration","Win32_SerialPortSetting","Win32_SMBIOSMemory","Win32_SoundDevice","Win32_SystemBIOS","Win32_SystemDriverPNPEntity","Win32_SystemEnclosure",
"Win32_SystemMemoryResource","Win32_SystemSlot","Win32_USBController","Win32_USBControllerDevice","Win32_USBHub","Win32_NetworkAdapter",
"Win32_NetworkAdapterConfiguration","Win32_NetworkAdapterSetting","Win32_Battery","Win32_CurrentProbe","Win32_PortableBattery",
"Win32_PowerManagementEvent","Win32_VoltageProbe","Win32_DriverForDevice","Win32_Printer","Win32_PrinterConfiguration","Win32_PrinterController",
"Win32_PrinterDriver","Win32_PrinterDriverDll","Win32_PrinterSetting","Win32_PrintJob","Win32_TCPIPPrinterPort","Win32_POTSModem","Win32_POTSModemToSerialPort",
"Win32_DesktopMonitor","Win32_DisplayConfiguration","Win32_DisplayControllerConfiguration","Win32_VideoConfiguration","Win32_VideoController","Win32_VideoSettings"};
foreach (string name in Names)
{
sw.WriteLine(" ");
sw.WriteLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
sw.WriteLine(name);
sw.WriteLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM " + name);
ManagementObjectCollection information = searcher.Get();
foreach (ManagementObject obj in information)
{
foreach (PropertyData data in obj.Properties)
{
sw.WriteLine("{0} = {1}", data.Name, data.Value);
}
}
}
sw.Close();
Where can I get this info?
Thanks!

You could try getting this information from the BIOS information, which is stored in the registry under the HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS. It should be in the SystemProductName or SystemVersion value.

Model and serial number contain in the "Win32_ComputerSystemProduct".

Related

Check if printer supports postscript

is there any way to check if printer supports postscript, using C#? I need to check this before I do anything with my document.
Thanks,
Bartosz
You could use WMI potentially, however im not sure if this solution will be reliable
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos = new ManagementObjectSearcher(oq);
ManagementObjectCollection moc = mos.Get();
foreach( ManagementObject mo in moc )
{
string name = mo["Name"].ToString();
string language = mo["DefaultLanguage"].ToString();
MessageBox.Show(String.Format("Printer: {0} -- Language: {1}", name, language));
}
Lifted from here
Update
Check here to see other fields that might be relevant
Win32_Printer class
In particular uint16 LanguagesSupported[];
Code, which I've finally used, with little changes:
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos = new ManagementObjectSearcher(oq);
ManagementObjectCollection moc = mos.Get();
foreach (ManagementObject mo in moc)
{
string name = mo["Name"].ToString();
var language = mo["LanguagesSupported"];
Console.WriteLine(String.Format("Printer: {0} -- Language: {1}", name, language==null ? 0 : (language as ushort[])[0]));
}

How to retrieve executable program path remotely

I'm writing a program that needs to get the install date and version information of all the programs in a registry. I am able to get a list of all of the programs, but I do not know how to access any information about the programs themselves. If I was able to determine the filepath to where the files are located I would be able to access this information. I happen to know that the programs I'm interested in are all located in the C:\\Program Files (x86)\ folder, but they are in subfolders within this that I am unable to specify. Any ideas on how to get the filepaths of the files I am retrieving?
Here's my code:
public List<BSAApp> getInstalledApps( string computerName )
{
List<BSAApp> appList = new List<BSAApp>();
ManagementScope ms = new ManagementScope();
ms.Path.Server = computerName;
ms.Path.NamespacePath = "root\\cimv2";
ms.Options.EnablePrivileges = true;
ms.Connect();
ManagementClass mc = new ManagementClass( "StdRegProv" );
mc.Scope = ms;
ManagementBaseObject mbo;
mbo = mc.GetMethodParameters( "EnumKey" );
mbo.SetPropertyValue( "sSubKeyName", "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths" );
string[] subkeys = (string[])mc.InvokeMethod( "EnumKey", mbo, null ).Properties["sNames"].Value;
if( subkeys != null )
{
foreach( string strKey in subkeys )
{
string path = ?????
FileVersionInfo info = FileVersionInfo.GetVersionInfo( path );
appList.Add( new BSAApp( strKey, info.ProductVersion ) );
}
}
return appList;
}
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach(ManagementObject mo in mos.Get())
{
Console.WriteLine(mo["Name"]);
Console.WriteLine(mo["InstallState"]);
}
Get installed applications in a system
But as mentioned in that thread, it has its own drawbacks.

How to detect freespace and disk information from one server to other in C#

If there are 3 pcs on network and if I want to detect the freespace and disk details of one pc from another pc then how to go about it...
I have found this code. but I don't know how should I test it inorder to know whether it is working.
is this the right way?
public Hashtable ReadFreeSpaceOnNetworkDrives()
{
//create Hashtable instance to hold our info
Hashtable driveInfo = new Hashtable();
//query the win32_logicaldisk for type 4 (Network drive)
SelectQuery query = new SelectQuery("select name, FreeSpace from win32_logicaldisk where drivetype=4");
//execute the query using WMI
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
//loop through each drive found
foreach (ManagementObject drive in searcher.Get())
{
//add the name & freespace to our hashtable
driveInfo.Add("Drive", drive["name"]);
driveInfo.Add("Space", drive["FreeSpace"]);
}
return driveInfo;
}
Update:
I got the answer to my question but I have got the code but it is in console application and I want in windows form application with a graphical representation of the disk space and drive info. How can I use this code and go about doing that?
ManagementScope scope = new ManagementScope("\\\\10.74.160.126\\root\\cimv2");
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)
{
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();
}
Console.ReadLine();
}
The code checks all the drives on the pc where you run this program. It returns a table with 2 entries per drive. One with the name and one with the free space. You can just write a simple program that uses this method and displays this data. It should be possible to query the drives from a remote computer. Maybe this article can tell you more http://msdn.microsoft.com/en-us/library/ms257337%28v=vs.80%29.aspx
EDIT:
public Hashtable ReadFreeSpaceOnNetworkDrives(String FullComputerName)
{
ManagementScope scope = new ManagementScope(fullComputerName);
scope.Connect();
//create Hashtable instance to hold our info
Hashtable driveInfo = new Hashtable();
//query the win32_logicaldisk for type 4 (Network drive)
SelectQuery query = new SelectQuery("select name, FreeSpace from win32_logicaldisk where drivetype=4");
//execute the query using WMI
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,query);
//loop through each drive found
foreach (ManagementObject drive in searcher.Get())
{
//add the name & freespace to our hashtable
driveInfo.Add("Drive", drive["name"]);
driveInfo.Add("Space", drive["FreeSpace"]);
}
return driveInfo;
}
covert into c#. and that may help you. http://www.codeguru.com/forum/showthread.php?t=426869

Get a list of all UNC shared folders on a local network server

I'm trying to get a list of all shared folders available on a local intranet server.
The System.IO.Directory.GetDirectories() works fine for a path like \\myServer\myShare, however I'm getting an exception for a path like \\myServer:
Unhandled Exception: System.ArgumentException: The UNC path should be of the form \server\share.
Is there a way to get a list all shared folders for a server? Ultimately I'm looking for a method that can handle both scenarios based on a given path - returning a list of all shares for a given server and returning a list of all subdirectories for a given network shared folder.
Here's a technique that uses System.Management (add a reference to this assembly):
using (ManagementClass shares = new ManagementClass(#"\\NameOfTheRemoteComputer\root\cimv2", "Win32_Share", new ObjectGetOptions())) {
foreach (ManagementObject share in shares.GetInstances()) {
Console.WriteLine(share["Name"]);
}
}
Appropriate permissions are required.
I think this is what you are looking for http://www.codeproject.com/KB/IP/networkshares.aspx
private DataTable GetSharedFolderAccessRule()
{
DataTable DT = new DataTable();
try
{
DT.Columns.Add("ShareName");
DT.Columns.Add("Caption");
DT.Columns.Add("Path");
DT.Columns.Add("Domain");
DT.Columns.Add("User");
DT.Columns.Add("AccessMask");
DT.Columns.Add("AceType");
ManagementScope Scope = new ManagementScope(#"\\.\root\cimv2");
Scope.Connect();
ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_LogicalShareSecuritySetting");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
ManagementObjectCollection QueryCollection = Searcher.Get();
foreach (ManagementObject SharedFolder in QueryCollection)
{
{
String ShareName = (String) SharedFolder["Name"];
String Caption = (String)SharedFolder["Caption"];
String LocalPath = String.Empty;
ManagementObjectSearcher Win32Share = new ManagementObjectSearcher("SELECT Path FROM Win32_share WHERE Name = '" + ShareName + "'");
foreach (ManagementObject ShareData in Win32Share.Get())
{
LocalPath = (String) ShareData["Path"];
}
ManagementBaseObject Method = SharedFolder.InvokeMethod("GetSecurityDescriptor", null, new InvokeMethodOptions());
ManagementBaseObject Descriptor = (ManagementBaseObject)Method["Descriptor"];
ManagementBaseObject[] DACL = (ManagementBaseObject[])Descriptor["DACL"];
foreach (ManagementBaseObject ACE in DACL)
{
ManagementBaseObject Trustee = (ManagementBaseObject)ACE["Trustee"];
// Full Access = 2032127, Modify = 1245631, Read Write = 118009, Read Only = 1179817
DataRow Row = DT.NewRow();
Row["ShareName"] = ShareName;
Row["Caption"] = Caption;
Row["Path"] = LocalPath;
Row["Domain"] = (String) Trustee["Domain"];
Row["User"] = (String) Trustee["Name"];
Row["AccessMask"] = (UInt32) ACE["AccessMask"];
Row["AceType"] = (UInt32) ACE["AceType"];
DT.Rows.Add(Row);
DT.AcceptChanges();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, ex.Message);
}
return DT;
}

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

Categories