Process Information

I tired below code, but all my values are empty not even zero
Now open Internet Explorer on you machine

using System;
using System.IO;
using System.Diagnostics;

static void Main(string[] args)
        {

            // Example 1

            Process[] ps = Process.GetProcessesByName("cmd.exe");
            Console.WriteLine("PS:", ps.Length);
            Console.WriteLine("PS_Count:", ps.Count());

            // Example 2

            PerformanceCounter pc1 = new PerformanceCounter();
            pc1.CategoryName = "Process";
            pc1.CounterName = "% Processor Time";
            pc1.InstanceName = "iexplore";
            pc1.MachineName = "machinename";
            string str = pc1.NextValue().ToString();
            Console.WriteLine("\n Process CPU:", str);
            Console.ReadLine();
 
        }


Output I am getting:
PS:
PS_Count:
Process CPU:

=======================================
I am even not getting zero value, I though from string str I can get value of CPU usage for IE process.

zeinthAsked:
Who is Participating?
 
käµfm³d 👽Commented:
You didn't include the format parameter ({0}) in your string:
    static void Main(string[] args)
    {
 
        // Example 1
 
        Process[] ps = Process.GetProcessesByName("cmd.exe");
        Console.WriteLine("PS: {0}", ps.Length);
 
        // Example 2
 
        PerformanceCounter pc1 = new PerformanceCounter();
        pc1.CategoryName = "Process";
        pc1.CounterName = "% Processor Time";
        pc1.InstanceName = "firefox";
        pc1.MachineName = ".";
        string str = pc1.NextValue().ToString();
        Console.WriteLine("\n Process CPU: {0}", str);
        Console.ReadLine();
    }

Open in new window

0
 
käµfm³d 👽Commented:
Also, process name takes just the name, no extension, so

    Process[] ps = Process.GetProcessesByName("cmd.exe");

should be

    Process[] ps = Process.GetProcessesByName("cmd");
0
 
zeinthAuthor Commented:
but why my string "str" was not giving me correct output ( there is no difference between your code and my code except the machine name attribute)
0
 
käµfm³d 👽Commented:
Actually, there is. If you compare line 17 of my post with the equivalent line in your post, you should see the difference.

Note the {0} in my post.
0
 
käµfm³d 👽Commented:
Explanation:

Console.WriteLine(),  as you have used it, takes as its arguments a format string and an additional parameter(s). Inside the format string, you can specify symbols such as {0} {1} {2} etc. to act as placeholders for the subsequent parameters. So if you wrote a line like

      Console.WriteLine("This {0} some {1}. Yes {2}{0}!!", "is", "text", "it");

on the console window you would see:

      This is some text. Yes it is!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.