Just omit the GetProcesssByName
part and call p.Kill()
. This will kill any process with "SoundCloud" in the main window title.
Close process containing specific string
-
15-07-2023 - |
Question
I Need to close a process containing the word "SoundCloud" I tried this but wont work it does show the messagebox with the full name but won't close the app the error system.indexoutofRangeException
private void getprocess()
{
StringBuilder sb = new StringBuilder();
richTextBox1.Clear();
int i = 1;
foreach (Process p in Process.GetProcesses("."))
{
try
{
if (p.MainWindowTitle.Length > 0)
{
if (p.MainWindowTitle.Contains("SoundCloud"))
{
timer1.Stop();
timer1.Enabled = false;
MessageBox.Show("Close "+p.MainWindowTitle.ToString());
Process[] proc = Process.GetProcessesByName(p.MainWindowTitle.ToString());
proc[0].Kill();
}
richTextBox1.AppendText("\n"+i.ToString()+" ). " + p.MainWindowTitle.ToString());
i++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow