Hi all,

I want to make a program that monitors a certain process by its name given by the user.

So the program starts at computer start up and asks you the name of the process that you want it to monitor( e.x. "nfsw.exe" or "klimn.exe" or etc.) then after 3 hours you start the process "klimn.exe".

I want the program to monitor all proceses until you start the klimn.exe process and then keep track of how much time you spent in that process, and writes that time in a file, after you closed the file.

The thing is that i don't know nothing about process monitoring, i looked on the web i found some like process audit but a friend of mine told me that there is a way easier way to do that.

My question is what do i have to look for?I only need indications, link, etc.I want to do this by my own.

My question is what do i have to look for?I only need indications, link, etc.I want to do this by my own.

Google for Windows Management Instrumentation (WMI)

I've gone this far but it seems that it doesnt write in the file,can please someone tell me why!

public Form1()
        {
            InitializeComponent();
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
            aTimer.Interval = 30000;
            aTimer.Enabled = true;
            MessageBox.Show("Aveti 30 de secunde sa scrieti numele procesului");
        }

        System.Timers.Timer aTimer;
        StreamWriter writeTest;
        Process[] runningProcess;

        void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            runningProcess = Process.GetProcesses();
                foreach (Process proc in runningProcess)
                {
                    if (proc.StartInfo.FileName == txtBoxProcess.Text)
                    {
                        writeTest = new StreamWriter(@"C:\Users\P4\Desktop\test.txt");
                        string temp = Convert.ToString(proc.StartTime.DayOfYear);
                        writeTest.WriteLine(temp);
                        writeTest.Close();
                    }
                }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            aTimer.Stop();

        }

You may also need performance counter

      processlist = Process.GetProcesses();
        cpuCounter = new PerformanceCounter();

The following will give you CPU usage at anytime
cpuCounter.CategoryName = "Process"; // Processor
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "Idle"; // "_Total";

To check for a specific process use

        foreach (Process aprocess in processlist)
        {
              if (aprocess.ProcessName.ToLower() == userprocessvar)

                  // code
          }

It is all in

            using System.Management;
            using System.Diagnostics;

it seems that it doesnt write in the file,can please someone tell me why!

I'll take a guess at this one.

    if (proc.StartInfo.FileName == txtBoxProcess.Text)


I believe the equality operator above needs an exact case sesnistive comparison of the two strings.  Thus, the strings must match perfectly so that the resultant Boolean value is true.

It might be that you're entering Svchost in your textbox but the actually name of the process is svchost. Notice the case difference in the two names which would mean that they do not match in case sesnitivity.  Therefore, you won't be able to write to the textfile.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.