VBForums >
.NET >
C# > [RESOLVED] [2003] How to use WMI queries dynamically
Click to See Complete Forum and Search --> : [RESOLVED] [2003] How to use WMI queries dynamically
Harsh Gupta
Sep 22nd, 2006, 04:40 PM
Hello,
When using VB, I use this code to get the name of all processes currently running;
Dim pro As Variant
For Each pro In GetObject("winmgmts:").ExecQuery("Select * from win32_process")
Debug.Print pro.Name
NextHow can I accomplish the same thing in C#?
Thank you.
jmcilhinney
Sep 22nd, 2006, 06:27 PM
You don't need WMI for that in either language. Just call Process.GetProcesses and then get the ProcessName property of each Process object in the array it returns.
Harsh Gupta
Sep 23rd, 2006, 04:17 AM
You don't need WMI for that in either language. Just call Process.GetProcesses and then get the ProcessName property of each Process object in the array it returns.
Thank you JM. I didn't know about the Process class.
But as per the original question, the C# code seems to be:
foreach(object pro in Interaction.GetObject(null,"winmgmts:").ExecQuery("Select * from Win32_Process"))
{
textBox1.Text = textBox1.Text + pro.name;
}
But it's giving an error:
'object' does not contain a definition for 'ExecQuery'
No help from MSDN either.
Another noobish question, in VB we use vbCrLf to goto next line. How to do it in C#?
Thank you
jmcilhinney
Sep 23rd, 2006, 08:06 PM
Interaction is a module in the Microsoft.VisualBasic namespace so you shouldn't be using it in C# at all to begin with.
Also, GetObject returns an Object reference, so if that code is compiling in VB then that would suggest that you have Option strict turned Off, which is also a bad idea under the majority of circumstances. In C#, or in VB with Option Strict turned On, you would have to cast the return value of GetObject to the appropriate type to access its ExecQuery method.
If you want to use WMI in .NET then you should be using the members of the System.Management namspace, including the ManagementClass and ManagementObject classes. That's for any .NET language, including VB.
vbCrLf is also not recommended. In VB Microsoft recommends ControlChars.NewLine, which is a constant, or you can also use Environment.NewLine. Many, myself included, prefer Environment.NewLine because it avoids the Microsoft.VisualBasic namespace altogether, which ControlChars is still a member of, and also it seems more correct because it uses the system definition of a line break rather than the VB definition. For that very reason though, Environment.NewLine is not a constant, so if you need a constant you should use ControlChars.NewLine. In C# you can also use Environment.NewLine but, because it's based on C syntax is also supports C escape sequences. That means you can insert a line break direct;y into a string, e.g."line 1\nline 2"The "\n" is the escape sequence for a new line. Note that if you want to be absolutely correct in a Windows sense then you should include a carriage return too:"line 1\r\nline 2"
Harsh Gupta
Sep 24th, 2006, 01:43 PM
Thank you JM.
But need a bit more information.
Interaction is a module in the Microsoft.VisualBasic namespace so you shouldn't be using it in C# at all to begin with.Why?
I understand, from your posts, that we should minimise the use of WMI, and if we want to, better import (or use as in C#) System.Management namespace. But then it is not dynamic or late binding to run WMI scripting.
Ex, there is a WMI script (no other option is available so I am running a WMI script) in my project and is used occasionally/rarely, where early binding is useless.
And as for the second question, sorry my question was incomplete. How to use NewLine within controls like TextBox or RichTextBox etc?? Anyways, ControlChars.NewLine or Environment.NewLine is the answer.
jmcilhinney
Sep 24th, 2006, 05:53 PM
Do you really think that C# would require you to reference the VB.NET Runtime to use WMI? Have a look at the ManagementObjectSearcher class.
As for line breaks in text boxes, or any controls for that matter, a string is a string and a line break is a line break. If you want to add a line break to a TextBox then you can use any of the methods I mentioned previously, e.g.[code]myTextBox.Text = "line 1";
myTextBox.AppendText("\nline 2");[code]Also not that text boxes, including the RTB, have a Lines property, so you can also assign a string array to that property and the elements will be joined with line breaks and displayed in the TextBox.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.