Actually all the information you got so far is correct:
- Task manager is just a tool to display data. It is not collecting anything
- In the background of task manager (and others like this) are system level tools, like WMI, as Mike Meinz highlighted.
- And you can access them easily also with performance counter tools provided by the .net framework as Sergey suggested.
Still, I would like to draw your attention towards built-in features you can use to achieve much the same result with no code at all from your side. The well-known PerfMon tool has features you can use to store your results into a database. Please see following article: http://www.sepago.de/e/nicholas/2009/11/02/performance-monitoring-part-7-using-performance-monitor-with-a-database