Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string kill = @"u([A-Za-z0-9-]+)";
           foreach (Process P in Process.GetProcessesByName(kill))
           {
               P.Kill();
           }


What I have tried:

This is my 1s try, So I need help on this Process kill.
Posted
Updated 15-Jun-16 15:21pm

GetProcessesByName takes the process name, not a regex that the process name matches.
You probably want something like this instead...
C#
foreach (Process P in Process.GetProcesses())
{
    if (P.ProcessName.StartsWith("U", StringComparison.CurrentCultureIgnoreCase))
        P.Kill();
}
 
Share this answer
 
C#
Process[] Processlist = Process.GetProcesses();
            foreach (var process in Processlist)
            {
                if (process.ProcessName.StartsWith("u"))
                {
                    process.Kill();
                }
            }



This is just work fine for me, I really appreciate for guys help. Thank you.
 
Share this answer
 
Comments
CHill60 16-Jun-16 5:03am    
See my comments to Solution 2 - this will NOT work if you have any processes beginning with capital-U (compare 'UNS' with 'unsecapp' for example)
Nigol_Learner 19-Jun-16 23:35pm    
I'm Understood bro, Thanks.
C#
Process[] Processlist= Process.GetProcesses().ToArray<process>();
foreach (var process in Processlist)
{
  if (process.ProcessName.StartWith("U")){
    process.Kill();
 }
            }</process>
 
Share this answer
 
v3
Comments
CHill60 15-Jun-16 10:48am    
Process.GetProcesses() returns a Process[] so there is no need for the .ToArray(). In any event your solution produces 5 compile errors:
- "</process>" ... take care when pasting code in
- ProcessName.StartWith? should be ProcessName.StartsWith
- ToArray<process>() - what is a "process"? I suspect you meant Process - which is completely redundant.
Fix all those issues and what you have left is Solution 1, with the exception that my solution will find (for example) both UNS and unsecapp but your's will only find UNS

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900