-1
private void Button_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "All files(*.*)|*.*";
        ofd.Multiselect = true;
        if (ofd.ShowDialog() == true)
        {
            MessageBox.Show("Files Selected");
            foreach(string filename in ofd.FileNames)
            {
                
            }
        }
    }

in this situation, i just wanna kill process which is selected by OpenFileDialog so that I can't use selectedprogram during this code is running... help me plz

00 0
  • 1
  • 4
    You can't select a process using an `OpenFileDialog`. It selects files, not processes. If you want to kill a process then you need to get a process, so you should be looking into how to do that. In doing so, you can see what relationship a process has to a file. – user18387401 Jun 14 '22 at 08:01
  • You can have multiple processes with the same application file, which would you pick? You can have processes which don't have any application file. Have you though about DLLs? – Charlieface Jun 14 '22 at 08:24

1 Answers1

0

As you can see from comments, the solution is partial, but you could search if among the running processes there are some with the same name as the selected files

Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "All files(*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == true)
{
    MessageBox.Show("Files Selected");
    foreach (string filename in ofd.FileNames)
    {
        var proc = System.Diagnostics.Process.GetProcesses().Where(w => w.ProcessName == System.IO.Path.GetFileNameWithoutExtension(filename)).FirstOrDefault();
        if (proc != null)
            proc.Kill(); // proc.Kill(true); to kill entire process tree
    }
}
Luca
  • 1,588
  • 2
  • 22
  • 26