Results 1 to 6 of 6

Thread: [RESOLVED] [2003] How to use WMI queries dynamically

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Resolved [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:
    1. Dim pro As Variant
    2.  
    3. For Each pro In GetObject("winmgmts:").ExecQuery("Select * from win32_process")
    4.     Debug.Print pro.Name
    5. Next
    How can I accomplish the same thing in C#?

    Thank you.
    Show Appreciation. Rate Posts.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [2003] How to use WMI queries dynamically

    Quote 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:
    1. foreach(object pro in Interaction.GetObject(null,"winmgmts:").ExecQuery("Select * from Win32_Process"))
    2.     {
    3.         textBox1.Text = textBox1.Text + pro.name;
    4.     }

    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
    Show Appreciation. Rate Posts.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Code:
    "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:
    Code:
    "line 1\r\nline 2"
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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.
    Show Appreciation. Rate Posts.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width