Check if printer supports postscript - c#

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]));
}

Related

C# can't take CPU,GPU,RAM,HDD InstallDate (f.e Win32_Processor, Win32_VideoController, Win32_PhysicalMemory, Win32_DiskDrive)

I can't get DateTime from InstallDate - CPU, GPU, RAM, HDD. Always appears an error System.NullReferenceException, or datetime like 01.01.0001 00.00.00
This is my code with 2 expample:
DateTime CpuDateTime, GpuDateTime; and etc.
ManagementObjectSearcher mos =
new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");
foreach (ManagementObject mo in mos.Get())
{
CpuDateTime = ManagementDateTimeConverter.ToDateTime(mo["InstallDate"].ToString());
}
ManagementObjectSearcher video =
new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_VideoController");
foreach (ManagementObject mo in video.Get())
{
GpuDateTime = ManagementDateTimeConverter.ToDateTime(mo["InstallDate"].ToString());
}
CpuTimeLabel.Text = CpuDateTime.ToString();
GpuTimeLabel.Text = GpuDateTime.ToString();
if i use this code : ManagementDateTimeConverter.ToDateTime(mo["InstallDate"].ToString()); i take this error - System.NullReferenceException
if i use this code without .ToString: ManagementDateTimeConverter.ToDateTime(mo["InstallDate"]); I take like this 01.01.0001 00.00.00
Please Help. Thank you very much

Setting printer Share name programmatically

I've been able to successfully rename a printer using the ManagementObject in the System.Management assembly.
string query = String.Format("SELECT * FROM Win32_Printer WHERE Name = '{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection collection = searcher.Get();
ManagementObject printer = collection.Cast<ManagementObject>().ElementAt(0);
printer.InvokeMethod("RenamePrinter", new object[] { newName });
Is there something similar to set the share name of a printer?
Here's a screenshot of the property I'm trying to change:
It turns out I can change the ManagementObject's properties directly. This is how I did it:
string query = String.Format("SELECT * FROM Win32_Printer WHERE Name = '{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection collection = searcher.Get();
ManagementObject printer = collection.Cast<ManagementObject>().ElementAt(0);
// The part that changes the printer share name
printer.Properties["ShareName"].Value = newName;
printer.Put();

What's wrong with my WMI query?

I'm trying to invert the following query:
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = 4856")) {
foreach (ManagementObject mo in searcher.Get()) {
Debug.WriteLine(mo["CommandLine"]);
}
}
Which returns the expected result:
C:\Windows\Explorer.EXE
INTO:
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT ProcessId FROM Win32_Process WHERE CommandLine = 'C:\\Windows\\Explorer.EXE'")) {
foreach (ManagementObject mo in searcher.Get()) {
Debug.WriteLine(mo["ProcessId"]);
}
}
Which throws an Invalid query exception and not the process id.
Ok, I just figured it out. I have to double up the slashes and escape characters in the path in the query.
Both the C# compiler and the WMI SQL implementation wants escaped slashes i guess. Stupid computers.
SELECT ProcessId FROM Win32_Process WHERE CommandLine = 'C:\\\\windows\\\\explorer.EXE'

System.Management.ManagementObjectSearcher

In order to retrieve some information of the target machine, I am using following code:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
string collectedInfo = ""; // here we will put the informa
foreach (ManagementObject share in searcher.Get())
{
// first of all, the processorid
collectedInfo += share.GetPropertyValue("ProcessorId").ToString ();
}
searcher.Query = new ObjectQuery("select * from Win32_BIOS");
foreach (ManagementObject share in searcher.Get())
{
//then, the serial number of BIOS
collectedInfo +=share.GetPropertyValue("SerialNumber").ToString ();
}
searcher.Query = new ObjectQuery("select * from Win32_BaseBoard");
foreach (ManagementObject share in searcher.Get())
{
//finally, the serial number of motherboard
collectedInfo+= share.GetPropertyValue("SerialNumber").ToString();
}
This works fine while it is executed as a asp.net website on my local computer, but as I publish it on another web server, it shows a different number.
Is there a way to make it calculate the users information, instead of calculating it for a server?

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

Categories