none
How to fetch task manager "Performance" tab values? RRS feed

  • Question

  • Hi all,

    I want to fetch the property values from task manager's performance tabs  like CPU usage,Memory ,Physical memory,system memory and system properties in c#.

    Please help me

    TIA


    sumitk

    Monday, November 13, 2017 7:04 PM

Answers

  • Hello Sumitk ,

    The PerformanceCounter class is a good choice for you, when you create a instance of PerformanceCounter it will associates with the specified system or custom performance counter and category instance on the local computer. And you will get some computer performance value from it.

    The PerformanceCounter has three important parameters as below.

    public PerformanceCounter (
     string categoryName,
     string counterName,
     string instanceName
    )

    The categoryName could be "Cache","Memory","Objects","PhysicalDisk","Process","Processor" and others.

    CounterName

    Gets or sets the name of the performance counter that is associated with this PerformanceCounter instance.

    InstanceName

    Gets or sets an instance name for this performance counter.

    For example, when you want to get available memory. you could try it by below.

    PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
    
    Console.WriteLine(  ramCounter.NextValue() + "MB");

    And if you don't know the available parameters of performanceCounter , please try the below code to get some information of category and  instance.

    public static void GetCategoryNameList()
            {
                PerformanceCounterCategory[] myCat2;
                myCat2 = PerformanceCounterCategory.GetCategories();
                for (int i = 0; i < myCat2.Length; i++)
                {
                    Console.WriteLine(myCat2[i].CategoryName);
                }
            }
    
            public static void ListCounters(string categoryName)
            {
                PerformanceCounterCategory category = PerformanceCounterCategory.GetCategories().First(c => c.CategoryName == categoryName);
                Console.WriteLine("{0} [{1}]", category.CategoryName, category.CategoryType);
    
                string[] instanceNames = category.GetInstanceNames();
    
                if (instanceNames.Length > 0)
                {
                    // MultiInstance categories
                    foreach (string instanceName in instanceNames)
                    {
                        ListInstances(category, instanceName);
                    }
                }
                else
                {
                    // SingleInstance categories
                    ListInstances(category, string.Empty);
                }
            }
    
            private static void ListInstances(PerformanceCounterCategory category, string instanceName)
            {
                Console.WriteLine("    {0}", instanceName);
                PerformanceCounter[] counters = category.GetCounters(instanceName);
    
                foreach (PerformanceCounter counter in counters)
                {
                    Console.WriteLine("        {0}", counter.CounterName);
                }
            }

    If you have any issues with it, please feel free to contact me.

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 14, 2017 7:08 AM
    Moderator

All replies

  • Hello Sumitk ,

    The PerformanceCounter class is a good choice for you, when you create a instance of PerformanceCounter it will associates with the specified system or custom performance counter and category instance on the local computer. And you will get some computer performance value from it.

    The PerformanceCounter has three important parameters as below.

    public PerformanceCounter (
     string categoryName,
     string counterName,
     string instanceName
    )

    The categoryName could be "Cache","Memory","Objects","PhysicalDisk","Process","Processor" and others.

    CounterName

    Gets or sets the name of the performance counter that is associated with this PerformanceCounter instance.

    InstanceName

    Gets or sets an instance name for this performance counter.

    For example, when you want to get available memory. you could try it by below.

    PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
    
    Console.WriteLine(  ramCounter.NextValue() + "MB");

    And if you don't know the available parameters of performanceCounter , please try the below code to get some information of category and  instance.

    public static void GetCategoryNameList()
            {
                PerformanceCounterCategory[] myCat2;
                myCat2 = PerformanceCounterCategory.GetCategories();
                for (int i = 0; i < myCat2.Length; i++)
                {
                    Console.WriteLine(myCat2[i].CategoryName);
                }
            }
    
            public static void ListCounters(string categoryName)
            {
                PerformanceCounterCategory category = PerformanceCounterCategory.GetCategories().First(c => c.CategoryName == categoryName);
                Console.WriteLine("{0} [{1}]", category.CategoryName, category.CategoryType);
    
                string[] instanceNames = category.GetInstanceNames();
    
                if (instanceNames.Length > 0)
                {
                    // MultiInstance categories
                    foreach (string instanceName in instanceNames)
                    {
                        ListInstances(category, instanceName);
                    }
                }
                else
                {
                    // SingleInstance categories
                    ListInstances(category, string.Empty);
                }
            }
    
            private static void ListInstances(PerformanceCounterCategory category, string instanceName)
            {
                Console.WriteLine("    {0}", instanceName);
                PerformanceCounter[] counters = category.GetCounters(instanceName);
    
                foreach (PerformanceCounter counter in counters)
                {
                    Console.WriteLine("        {0}", counter.CounterName);
                }
            }

    If you have any issues with it, please feel free to contact me.

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 14, 2017 7:08 AM
    Moderator
  • Task Manager uses NtQuerySystemInformation() to get all those data.
    • Proposed as answer by Fei HuModerator Wednesday, November 15, 2017 9:58 AM
    Tuesday, November 14, 2017 7:50 AM