I'm using ManagementObjectSearcher to load all the printers available in the network. There all the printers are returned in a ManagementObjectCollection. Is there anyway to find out all the details returned?
I used the debugging of c# to preview the object but it does not show all the data in there. I want to know what is available other than Printers[Name],Printers[Local],Printers[Network]. Is there a possible way to do this?
Code
System.Management.ManagementScope objMS =
new System.Management.ManagementScope(ManagementPath.DefaultPath);
objMS.Connect();
SelectQuery objQuery = new SelectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher objMOS = new ManagementObjectSearcher(objMS, objQuery);
System.Management.ManagementObjectCollection objMOC = objMOS.Get();
foreach (ManagementObject Printers in objMOC)
{
System.Management.PropertyDataCollection pdc = Printers.Properties;
if (Convert.ToBoolean(Printers["Local"])) // LOCAL PRINTERS.
{
comboBox8.Items.Add(Printers["Name"]);
}
if (Convert.ToBoolean(Printers["Network"])) // ALL NETWORK PRINTERS.
{
comboBox9.Items.Add(Printers["Name"]);
}
}
Generate strongly typed classes for Win32_Printer that will show you everything you need.
http://msdn.microsoft.com/en-us/library/2wkebaxa(v=vs.110).aspx - Mgmtclassgen.exe (Management Strongly Typed Class Generator)
You can enumerate the ManagementBaseObject.Properties property, and PropertyData.Qualifiers.
foreach (PropertyData property in properties)
{
Console.WriteLine(property.Name);
foreach (QualifierData q in property.Qualifiers)
{
if(q.Name.Equals("Description"))
{
Console.WriteLine(
processClass.GetPropertyQualifierValue(
property.Name, q.Name));
}
}
Console.WriteLine();
}
From MSDN
Related
we have a print server and lots of printers on it. We access them like this:
\\print-server-name\printer1
\\print-server-name\printer1_color
\\print-server-name\printer2
...etc.
I now need a list of all printers on that server. Until now, I could only find all printers installed locally on the machine. I found this while googling which gave me only the local printers aswell:
PrintServer lps = new PrintServer();
PrintQueueCollection prQueue = lps.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Connections });
liServerPrinterNames = (from printer in prQueue select printer).ToList();
Actually I only need the names of all printers on the server in a string array, I don't even need objects for interaction, only the names of all printers as string. Is that possible? How?
Thanks for any help in advance!
Gets all InstalledPrinters
foreach (string printer in PrinterSettings.InstalledPrinters)
Using WMI Windows Management Instrumentation
SelectQuery query = new SelectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos= new ManagementObjectSearcher(mos, query);
System.Management.ManagementObjectCollection moc= mos.Get();
foreach (ManagementObject Printers in moc )
Printers["Name"]; //GetPrinterName
}
Here is how I did it:
using System.Printing;
string serverAddress = #"\\server.domain.local"
PrintServer printServer = new PrintServer($#"{serverAddress}")
PrintQueueCollection queues = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local });
foreach (var item in queues)
{
Console.WriteLine(item.Name)
}
If you want the full name (\\server.domain.local\PrinterName) use property FullName instead of Name :)
I've developed a printer search application that search for all online printers on the network. Im referring to this site - here
The button event handler is something like:
private void btnGetPrinters_Click(object sender, EventArgs e)
{
// Use the ObjectQuery to get the list of configured printers
System.Management.ObjectQuery oquery =
new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
System.Management.ManagementObjectSearcher mosearcher =
new System.Management.ManagementObjectSearcher(oquery);
System.Management.ManagementObjectCollection moc = mosearcher.Get();
foreach (ManagementObject mo in moc)
{
System.Management.PropertyDataCollection pdc = mo.Properties;
foreach (System.Management.PropertyData pd in pdc)
{
if ((bool)mo["Network"])
{
cmbPrinters.Items.Add(mo[pd.Name]);
}
}
}
}
but it's not working. I've debugged by line an found that the ((bool)mo["Network"]) cond. return false. Anyone have idea why this happen? I've checked my network and printers conn. It's all working fine. please advice.
I know we can get the BIOS information using system.management assembly but the assembly is not accessible for windows 8 app. I specifically need to know the serial number of the laptop on which the app is running. Is there any way that I can access that ?
I don't think there is a way if you are developing a Windows Modern UI App.
Modern UI Apps get run in a sandbox environment which have very limited access to anything. Check MSDN documentations on that.
If you are developing a desktop Windows app on the other hand, then try the following code:
(You need to import System.Management.dll into your project.)
using System;
using System.IO;
using System.Management;
namespace GetHardwareIds
{
internal class Program
{
private static void Main(string[] args)
{
using (StreamWriter writer = new StreamWriter(#"C:\HardwareInfo.txt"))
{
using
(
ManagementObjectSearcher searcher =
// Where __Superclass Is Null: selects only top-level classes.
// remove it if you need a list of all classes
// new ManagementObjectSearcher("Select * From meta_class Where __Superclass Is Null")
// this query only select the processor info. for more options uncomment top line
new ManagementObjectSearcher("Select * From meta_class Where __Class = 'Win32_Processor'")
)
{
foreach (ManagementObject managementObject in searcher.Get())
{
Console.WriteLine(managementObject.Path.ClassName);
writer.WriteLine(managementObject.Path.ClassName);
GetManagementClassProperties(managementObject.Path.ClassName, writer);
managementObject.Dispose();
}
}
}
}
public static void GetManagementClassProperties(string path, StreamWriter writer)
{
using (ManagementClass managementClass = new ManagementClass(path))
{
foreach (ManagementObject instance in managementClass.GetInstances())
{
foreach (PropertyData property in instance.Properties)
{
Console.WriteLine(" {0} = {1}", property.Name, property.Value);
writer.WriteLine(" {0} = {1}", property.Name, property.Value);
}
instance.Dispose();
}
}
}
}
}
Check this code. I am not a 100% clear on what you are trying to achieve but this code should return the device ID specified by Win8 (this code includes a concatenation of all ids.)
// get hardware token
HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
// get hardware ID bytes
byte[] idBytes = hwToken.Id.ToArray();
// populate device ID as a string value
string deviceID = string.Join(",", idBytes);
Here is the link to MSDN articles about it:
http://msdn.microsoft.com/en-us/library/windows/apps/jj553431.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.profile.hardwareidentification.getpackagespecifictoken.aspxThere is an entry for BIOS in the return structure based on these articles.
Hopefully, this does what you need. Let me know if it worked :)
Unfortunately the information you want to obtain is not available to WinRT applications.
how can I turn off 'allow hybrid sleep' in advanced power setting? by c#
by manually: power options -> change Plan Settings -> change advanced power setting->
Sleep-> 'Allow hybrid sleep' -> plugged in: OFF
If you are targeting Windows 7/2008 Server then you can use WMI and the Win32_PowerSetting class. Below is code that does that. Make sure to add an assembly reference and using directive to System.Management.
private bool SetAllowHybridSleep(bool enabled)
{
//Machine to work on, "." for local
string RemotePC = ".";
//Set the namespace to the power namespace, used throughout the function
ManagementScope ms = new ManagementScope(#"\\" + RemotePC + #"\root\cimv2\power");
//Will hold each of our queries
ObjectQuery oq = null;
//Will hold the values of our power plan and the specific setting that we want to change
Guid PowerPlanInstanceId = Guid.Empty;
string PowerSettingInstanceId = null;
//Look for the specific setting that we want
oq = new ObjectQuery(string.Format("SELECT * FROM Win32_PowerSetting WHERE ElementName = 'Allow hybrid sleep'"));
using (ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq))
{
ManagementObjectCollection results = mos.Get();
foreach (ManagementObject obj in results)
{
foreach (PropertyData p in obj.Properties)
{
if (p.Name == "InstanceID")
{
//This will give us a string with a GUID specific to our setting
PowerSettingInstanceId = p.Value.ToString();
break;
}
}
}
}
//Sanity check
if (string.IsNullOrEmpty(PowerSettingInstanceId))
{
Console.WriteLine("System does not support hybrid sleep");
return false;
}
//Look for the active power scheme
oq = new ObjectQuery("SELECT * FROM Win32_PowerPlan WHERE IsActive=True");
using (ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq))
{
ManagementObjectCollection results = mos.Get();
foreach (ManagementObject obj in results)
{
foreach (PropertyData p in obj.Properties)
{
if (p.Name == "InstanceID")
{
//The instance contains a string with a GUID inside of it, use the code below to get the GUID by itself
if (!Guid.TryParse(System.Text.RegularExpressions.Regex.Match(p.Value.ToString(), #"\{[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}\}").Value, out PowerPlanInstanceId))
{
Console.WriteLine("Could not find active power plan");
return false;
}
break;
}
}
}
}
//Now we need to update the actual power setting in the active plan
//Get all power schemes for the target setting
oq = new ObjectQuery(string.Format("ASSOCIATORS OF {{Win32_PowerSetting.InstanceID=\"{0}\"}} WHERE ResultClass = Win32_PowerSettingDataIndex", PowerSettingInstanceId.Replace(#"\", #"\\")));
using (ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq))
{
ManagementObjectCollection results = mos.Get();
foreach (ManagementObject obj in results)
{
foreach (PropertyData p in obj.Properties)
{
//See if the current scheme is the current setting. This will happen twice, once for AC and once for DC
if (p.Name == "InstanceID" && p.Value.ToString().Contains(PowerPlanInstanceId.ToString()))
{
//Change the value of the current setting
obj.SetPropertyValue("SettingIndexValue", (enabled ? "1" : "0"));
obj.Put();
break;
}
}
}
}
return true;
}
Using procmon, I managed to work out that the following registry key is responsible for it on my machine.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\94ac6d29-73ce-41a6-809f-6363ba21b47e
You'll probably have to do some research on your machine to see how it works for you.
You can also call on the powercfg utility to do this. Each power setting is identified by three things:
GUID of power profile
GUID of profile subgroup
GUID of setting
You can use powercfg -QUERY to produce a full list of values.
Once you've got the GUID of the profile you want to edit, the GUID of the subgroup (in this case the Sleep subgroup) and the GUID of the setting (Allow Hybrid Sleep) you can use either powercfg -SETACVALUEINDEX for plugged in or powercfg -SETDCVALUEINDEX for on battery to set the value.
In my case (Win7 Ultimate x64) you can turn it off using:
powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 238c9fa8-0aad-41ed-83f4-97be242c8f20 94ac6d29-73ce-41a6-809f-6363ba21b47e 1
This translates to the AcSettingIndex value in:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\94AC6D29-73CE-41A6-809F-6363BA21B47E\DefaultPowerSchemeValues\381b4222-f694-41f0-9685-ff5bb260df2e
I am trying to get the printer status of a PointOfSale printer using the following code:
Hashtable properties = new Hashtable();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win2_Printer");
foreach (ManagementObject obj in searcher.Get()) {
if (obj["name"].ToString() == printerName) {
foreach (PropertyData data in obj.Properties) {
if(data.Name.ToLower() = "printerstatus") {
int printerStatus = Convert.ToInt32(data.Value);
}
}
}
}
Problem is, the status is either 3 (idle) or 4(printing), even when unplugged or the paper is out.
I have read a lot of posts with this same issue, but have not found an answer. Is this correct? How else would I check the status? Any help is appreciated.
What Brand of printer are you using?
Sometimes the Brand will have a specific command you can send to query the status.