2

Im looking for a way to kill a process by name using C# on WindowsCE

The .NET Compact Framework doesnt have a Process.GetProcessByName() method it only has a .GetProcessById() method.

But i dont know how i can figure out the process id of my running process.

I was thinking i could loop though all the process ids, but this is horrible because i dont know the max number of process ids. Anyone know a better way?

        for (int i = 1; i < 40000; i++)
        {
            Process prs = Process.GetProcessById(i);

            if (prs.StartInfo.FileName == "MyExe.exe")
            {
                prs.Kill();
            }

            prs.Dispose();
        }

EDIT: I found the solution to my problem. A codeproject link. http://www.codeproject.com/Articles/36841/Compact-Framework-Process-class-that-supports-full

CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • this may be a duplicate.. have a look at this http://stackoverflow.com/questions/7307998/how-to-kill-process-in-windows-ce- (one guy links to http://msdn.microsoft.com/en-us/library/aa446560.aspx) –  May 13 '13 at 14:06
  • A better alternative would be to just use [`Process.GetProcesses()`](http://msdn.microsoft.com/en-us/library/1f3ys1f9.aspx) and loop through those. – Daniel Hilgarth May 13 '13 at 14:08
  • @DanielHilgarth That method is not available in .Net Compact Framework – CathalMF May 13 '13 at 14:17

1 Answers1

6

I found a Code Project class which does exactly what i need it to do. Ill post it as an answer to this project.

http://www.codeproject.com/Articles/36841/Compact-Framework-Process-class-that-supports-full

        ProcessInfo[] list = ProcessCE.GetProcesses();

        foreach (ProcessInfo pinfo in list)
        {
            if (pinfo.FullPath.EndsWith("MyExe.exe"))
                pinfo.Kill();
        }
CathalMF
  • 9,705
  • 6
  • 70
  • 106