hi,
Is it possible to query WMI to take only the owner of the processes?
My test, but it does not work:
ObjectQuery query = new ObjectQuery
("Select * from Win32_Process where Owner = 'LolaLola'");
hi,
Is it possible to query WMI to take only the owner of the processes?
My test, but it does not work:
ObjectQuery query = new ObjectQuery
("Select * from Win32_Process where Owner = 'LolaLola'");
As far as I know you have to iterate all processes and call .GetOwner()
in WMI. This is a very slow way to iterate process.
see this post:
http://www.daniweb.com/forums/thread249799.html
See this article:
http://www.codeproject.com/KB/cs/processownersid.aspx
When you use WMI to query running process you can use a WHERE clause when there is an available attribute, unfortunately Owner is not one of the attributes, there may be a way to resolve the process ID or Session ID to a specific Owner, possibly? At any rate, Here is some code that will retrieve every running process on the machine and under each process will be all of the attributes specific to the process as a child node, Create a windows form and add a Button and TreeView Control, then paste the following code accordingly, Hope this helps.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace PrinterQuery
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
TreeNode ParentNode, Node;
ManagementClass Class = new ManagementClass("\\\\127.0.0.1\\root\\CIMV2:Win32_Process");
Class.Options.UseAmendedQualifiers = true;
Class.Options.Timeout = new TimeSpan(0, 0, 0, 3, 0);
PropertyDataCollection Properties = Class.Properties;
if (Class.Properties.Count > 0)
{
foreach (ManagementObject Result in Class.GetInstances())
{
ParentNode = TreeView1.Nodes.Add(Result.Properties["Name"].Value.ToString());
foreach (PropertyData Property in Result.Properties)
{
Node = ParentNode.Nodes.Add(Property.Name);
if (Result.GetPropertyValue(Property.Name) != null)
{
if (Result.GetPropertyValue(Property.Name).GetType().IsArray)
AddADTreeArrayValues(Node, new ResultProperty(Result.GetPropertyValue(Property.Name)));
else Node.Nodes.Add(GetMOString(new ResultProperty(Property.Name), Result));
}
else Node.Nodes.Add("Null");
}
}
TreeView1.Sort();
}
}
private string GetMOString(ResultProperty Property, ManagementObject Result)
{
return Result.GetPropertyValue(Property.String) != null ? Result.GetPropertyValue(Property.String).ToString() : "Null";
}
private void AddADTreeArrayValues(TreeNode ParentNode, ResultProperty Property) //Some WMI attributes are returned as arrays.
{
TreeNode ArrayIDNode;
if (Property.Type == typeof(string[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.String[" + Property.StringArray.Length.ToString() + "]");
foreach (string s in Property.StringArray)
if (s.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(s));
else ArrayIDNode.Nodes.Add(s);
}
else if (Property.Type == typeof(byte[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.Byte[" + Property.ByteArray.Length.ToString() + "]");
foreach (byte b in Property.ByteArray)
if (b.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(b));
else ArrayIDNode.Nodes.Add(b.ToString());
}
else if (Property.Type == typeof(int[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.Int[" + Property.INTArray.Length.ToString() + "]");
foreach (int i in Property.INTArray)
if (i.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(i));
else ArrayIDNode.Nodes.Add(i.ToString());
}
else if (Property.Type == typeof(ushort[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.UInt16[" + Property.UShortArray.Length.ToString() + "]");
foreach (ushort u in Property.UShortArray)
if (u.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(u));
else ArrayIDNode.Nodes.Add(u.ToString());
}
else if (Property.Type == typeof(object[]))
{
ArrayIDNode = ParentNode.Nodes.Add("System.Object[" + Property.ObjectArray.Length.ToString() + "]");
foreach (object o in Property.ObjectArray)
if (o.GetType().IsArray) AddADTreeArrayValues(ArrayIDNode, new ResultProperty(o));
else ArrayIDNode.Nodes.Add(o.ToString());
}
}
}
struct ResultProperty
{
object _Value;
bool _IsArray;
string[] _StringArray;
object[] _ObjectArray;
byte[] _ByteArray;
int[] _IntArray;
ushort[] _UShortArray;
Type _T;
public ResultProperty(object Value)
{
_Value = Value;
_IsArray = Value.GetType().IsArray;
_T = Value.GetType();
_StringArray = null;
_ObjectArray = null;
_ByteArray = null;
_IntArray = null;
_UShortArray = null;
}
public string String { get { return GetString(); } }
public int INT { get { return GetInt(); } }
public Int64 INT64 { get { return GetInt64(); } }
public DateTime Date { get { return GetDate(); } }
public object Value { get { return _Value; } }
public bool IsArray { get { return _IsArray; } }
public string[] StringArray { get { return ToStringArray(); } }
public byte[] ByteArray { get { return ToByteArray(); } }
public int[] INTArray { get { return ToIntArray(); } }
public ushort[] UShortArray { get { return ToUShortArray(); } }
public object[] ObjectArray { get { return ToObjectArray(); } }
public Type Type { get { return GetTypeOf(); } }
private Type GetTypeOf()
{
try { return _T; }
catch { throw new Exception("Unable to retrieve T type"); }
}
private string GetString()
{
try { return Convert.ToString(_Value); }
catch { return null; }
}
private DateTime GetDate()
{
try { return Convert.ToDateTime(_Value); }
catch { return new DateTime(); }
}
private Int64 GetInt64()
{
try { return Convert.ToInt64(_Value); }
catch { return Int64.MinValue; }
}
private int GetInt()
{
try { return Convert.ToInt32(_Value); }
catch { return int.MinValue; }
}
private int GetInt(object value)
{
try { return Convert.ToInt32(value); }
catch { return int.MinValue; }
}
private UInt16 GetUInt16()
{
try { return Convert.ToUInt16(_Value); }
catch { return ushort.MinValue; }
}
private ushort GetUInt16(object value)
{
try { return Convert.ToUInt16(value); }
catch { return ushort.MinValue; }
}
private byte[] ToByteArray()
{
if (!(_IsArray && Value.GetType() == typeof(byte[]))) return null;
try
{
byte[] barray = (byte[])Value;
List<byte> bytearray = new List<byte>();
foreach (byte b in barray) bytearray.Add(b);
return _ByteArray = bytearray.ToArray();
}
catch
{
return null;
}
}
private int[] ToIntArray()
{
if (!_IsArray) return null;
try
{
int[] iarray = (int[])Value;
List<int> intarray = new List<int>();
foreach (int i in iarray) intarray.Add(GetInt(i));
return _IntArray = intarray.ToArray();
}
catch
{
return null;
}
}
private object[] ToObjectArray()
{
if (!_IsArray) return new object[] { _Value };
try
{
object[] objectarray = (object[])_Value;
List<object> oarray = new List<object>();
foreach (object o in objectarray) oarray.Add(o != null ? o.ToString() : "");
return _ObjectArray = oarray.ToArray();
}
catch
{
return null;
}
}
private ushort[] ToUShortArray()
{
if (!_IsArray) return null;
try
{
ushort[] UShortArray = (ushort[])Value;
List<ushort> uarray = new List<ushort>();
foreach (ushort u in UShortArray) uarray.Add(GetUInt16(u));
return _UShortArray = uarray.ToArray();
}
catch
{
return null;
}
}
private string[] ToStringArray()
{
if (_StringArray != null) return _StringArray;
if (!(_IsArray && (Value.GetType() == typeof(string[]) || Value.GetType() == typeof(object[])))) return new string[] { Value.ToString() };
try
{
object[] oarray = (object[])Value;
List<string> outarray = new List<string>();
foreach (object obj in oarray) outarray.Add(obj != null ? obj.ToString() : "");
return _StringArray = outarray.ToArray();
}
catch
{
return null;
}
}
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.