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();
Related
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]));
}
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'
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?
I have tried two ways to accomplish this so far.
The first way, I used System.Diagnostics, but I get a NotSupportedException of "Feature is not supported for remote machines" on the MainModule.
foreach (Process runningProcess in Process.GetProcesses(server.Name))
{
Console.WriteLine(runningProcess.MainModule.FileVersionInfo.FileDescription);
}
The second way, I attempted using System.Management but it seems that the Description of the ManagementObject is the she same as the Name.
string scope = #"\\" + server.Name + #"\root\cimv2";
string query = "select * from Win32_Process";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject obj in collection)
{
Console.WriteLine(obj["Name"].ToString());
Console.WriteLine(obj["Description"].ToString());
}
Would anyone happen to know of a better way to go about getting the descriptions of a running process on a remote machine?
Well I think I've got a method of doing this that will work well enough for my purposes. I'm basically getting the file path off of the ManagementObject and getting the description from the actual file.
ConnectionOptions connection = new ConnectionOptions();
connection.Username = "username";
connection.Password = "password";
connection.Authority = "ntlmdomain:DOMAIN";
ManagementScope scope = new ManagementScope(#"\\" + serverName + #"\root\cimv2", connection);
scope.Connect();
ObjectQuery query = new ObjectQuery("select * from Win32_Process");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject obj in collection)
{
if (obj["ExecutablePath"] != null)
{
string processPath = obj["ExecutablePath"].ToString().Replace(":", "$");
processPath = #"\\" + serverName + #"\" + processPath;
FileVersionInfo info = FileVersionInfo.GetVersionInfo(processPath);
string processDesc = info.FileDescription;
}
}
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;
}