Here is my list:
public static List<Tuple<string, string>> hardDiskInfo(string hostname)
{
var hardDiskInfo = new List<Tuple<string, string>>();
ManagementScope Scope;
if (!hostname.Equals("localhost", StringComparison.OrdinalIgnoreCase))
{
ConnectionOptions Conn = new ConnectionOptions();
Conn.Username = Properties.Settings.Default.uName;
Conn.Password = Properties.Settings.Default.pWord;
Conn.Authority = "ntlmdomain:" + Properties.Settings.Default.doMain;
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", hostname), Conn);
}
else
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", hostname), null);
Scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3 OR DriveType = 4");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject mo in queryCollection)
{
foreach (PropertyData p in mo.Properties)
{
if (p.Value != null)
{
hardDiskInfo.Add(new Tuple<string, string>(p.Name.ToString(), p.Value.ToString()));
}
}
}
return hardDiskInfo;
}
I'd like to know how to get the second p.Value of the p.Name after calling it:
hardDiskInfo(inputText.Text);
For example the value of the "FreeSpace" which is defined in Win32_LogicalDisk.
I'm having more Win32_ queries so knowing this will help me handling all of them and I'll be a happy panda.
Thank you.
Are the Names unique?
You might try:
var values = hardDiskInfo(inputText.Text);
// Get the first or default which matches "FreeSpace".
var freeSpaceInfo = values.FirstOrDefault(item => item.Item1 == "FreeSpace");
// If it was found,
if(freeSpaceInfo != null)
{
MessageBox.Show($"FreeSpace: {freeSpaceInfo.Item2}");
}
Next step: Use a Dictionary<string, string> which is much better.
Related
So I am trying to get Description information from Win32_SystemDriver into a RichTextBox, but I am not able to do that because it only displays the last result from the query. As you can see bellow I tried to build an array but it does not work.
ObjectQuery query8 = new ObjectQuery(
"SELECT * FROM Win32_SystemDriver");
ManagementObjectSearcher searcher8 =
new ManagementObjectSearcher(scope, query8);
foreach (ManagementObject queryObj in searcher8.Get())
{
string[] arrTeamMembers = new string[] { queryObj["Description"].ToString() };
foreach (var item in arrTeamMembers)
{
richTextBox1.Text = item;
}
}
Do you have any ideia how can I display all the info listing into the RichTextBox?
Try following :
List<string> arrTeamMembers = new List<string>();
foreach (ManagementObject queryObj in searcher8.Get())
{
arrTeamMembers.Add(queryObj["Description"].ToString());
}
richTextBox1.Text = string.Join(",", arrTeamMembers);
Your approach is ok. There is one thing which you missed: richTextBox1.Text is a so-called property that stores a string. Now the reason why it shows you only the last driver is that you set this property to a new value for each driver you have in your array. So for the first driver it sets it to "driverA" and for the second to "driverB". What you are looking for is the += operator --> richTextBox1.Text += item;. If you want to add spaces between the drivers you can do something like richTextBox1.Text += $" {item}";. This way you have a leading whitespace but formating is personal preference.
Please note that ManagementObjectSearcher are IDisposable and therefore should be disposed.
ObjectQuery query8 = new ObjectQuery("SELECT * FROM Win32_SystemDriver");
using (ManagementObjectSearcher searcher8 = new ManagementObjectSearcher(scope, query8))
{
List<string> arrTeamMembers = new List<string>();
foreach (ManagementObject queryObj in searcher8.Get())
{
arrTeamMembers.Add(queryObj["Description"].ToString());
}
richTextBox1.Text = string.Join(Environment.NewLine, arrTeamMembers);
}
var query8 = new ObjectQuery("SELECT * FROM Win32_SystemDriver");
var searcher8 = new ManagementObjectSearcher(scope, query8);
var strbuilder = new StringBuilder();
foreach (var queryObj in searcher8.Get())
strbuilder.AppendLine($"{queryObj["Description"].ToString()}");
richTextBox1.Text = strbuilder.ToString();
How to get a list users in Task Manager with status?
I found only how to get a list of domain users
var usersSearcher = new ManagementObjectSearcher(#"SELECT * FROM Win32_UserAccount");
var users = usersSearcher.Get();
You can try this code to get the list of users:
var usersSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_UserAccount");
var managementObjects = usersSearcher.Get();
List<string> result = new List<string>();
foreach (ManagementObject item in managementObjects)
{
foreach (var pr in item.Properties)
{
if (pr.Name == "Caption")
{
result.Add(pr.Value?.ToString());
}
}
}
var users = result.Distinct().ToList();
Also you may try this:
var usersSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Process");
var managementObjects = usersSearcher.Get();
List<string> allUsers = new List<string>();
foreach (ManagementObject obj in managementObjects)
{
string[] argList = new string[] { string.Empty, string.Empty };
int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
if (returnVal == 0)
{
// return DOMAIN\user
allUsers.Add(argList[1] + "\\" + argList[0]);
}
}
var result = allUsers.Distinct().ToList();
I used this code for finding graphic cards:
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_VideoController");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
string graphicsCard = "";
foreach (ManagementObject mo in queryCollection)
{
foreach (PropertyData property in mo.Properties)
{
if (property.Name == "Description")
{
graphicsCard += property.Value.ToString() + " ";
}
}
}
I have two graphic cards:
Above code return all graphic cards.
How to find active graphic card that chosen by windows?
try this
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
string graphicsCard = string.Empty;
foreach (ManagementObject obj in searcher.Get())
{
if (obj["CurrentBitsPerPixel"] != null && obj["CurrentHorizontalResolution"] != null)
{
graphicsCard = obj["Name"].ToString();
}
}
I'm trying to update a WMI instance using C# following one example in MSDN but I cannot get it to work. It is firing me a 'System.Management.ManagementException' which does not give me any answer. Can you please tell me if I'm doing something wrong?
public void UpdateInstance(string parametersJSON)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
object result = serializer.Deserialize(parametersJSON, typeof(object));
Dictionary<string, object> dic = (Dictionary<string, object>)result;
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOnly;
ManagementObject objHostSetting = new ManagementObject();
objHostSetting.Scope = new ManagementScope("root\\onguard");
objHostSetting.Path = new ManagementPath("Lnl_Cardholder.SSNO = '33263085'"); // This is the line that fires the exception
foreach (KeyValuePair<string, object> value in dic)
{
objHostSetting[value.Key] = value.Value.ToString();
}
//update the ManagementObject
objHostSetting.Put(options);
}
I changed my code to this and now works:
public void UpdateInstance(string scope, string query, string parametersJSON)
{
WindowsImpersonationContext impersonatedUser = WindowsIdentity.GetCurrent().Impersonate();
JavaScriptSerializer serializer = new JavaScriptSerializer();
object result = serializer.Deserialize(parametersJSON, typeof(object));
Dictionary<string, object> dic = (Dictionary<string, object>)result;
ManagementObjectSearcher searcher;
searcher = new ManagementObjectSearcher(scope, query);
EnumerationOptions options = new EnumerationOptions();
options.ReturnImmediately = true;
foreach (ManagementObject m in searcher.Get())
{
foreach (KeyValuePair<string, object> k in dic)
{
m.Properties[k.Key].Value = k.Value;
}
m.Put();
}
}
I'm trying to get some info about RSOP_SecuritySettingBoolean but it returns an empty collection. Am i doing something wrong? Win7 x64 HP without domain:
var options = new ConnectionOptions();
var scope = new ManagementScope(#"\\.\root\RSOP\Computer", options);
var objectQuery = new ObjectQuery("SELECT * FROM RSOP_SecuritySettingBoolean");
using (var searcher = new ManagementObjectSearcher(scope, objectQuery))
{
foreach (ManagementObject o in searcher.Get())
{
Console.WriteLine("Key Name: {0}", o["KeyName"]);
Console.WriteLine("Precedence: {0}", o["Precedence"]);
Console.WriteLine("Setting: {0}", o["Setting"]);
}
}