|
-
Sep 22nd, 2006, 04:40 PM
#1
[RESOLVED] [2003] How to use WMI queries dynamically
Hello,
When using VB, I use this code to get the name of all processes currently running;
VB Code:
Dim pro As Variant
For Each pro In GetObject("winmgmts:").ExecQuery("Select * from win32_process")
Debug.Print pro.Name
Next
How can I accomplish the same thing in C#?
Thank you.
-
Sep 22nd, 2006, 06:27 PM
#2
Re: [2003] How to use WMI queries dynamically
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.
-
Sep 23rd, 2006, 04:17 AM
#3
Re: [2003] How to use WMI queries dynamically
 Originally Posted by jmcilhinney
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:
VB Code:
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:
Code:
'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
-
Sep 23rd, 2006, 08:06 PM
#4
Re: [2003] How to use WMI queries dynamically
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.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:
-
Sep 24th, 2006, 01:43 PM
#5
Re: [2003] How to use WMI queries dynamically
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.
-
Sep 24th, 2006, 05:53 PM
#6
Re: [2003] How to use WMI queries dynamically
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|