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)
{
}
}
}在这种情况下,我只想杀死由OpenFileDialog选择的进程,这样在代码运行期间我就不能使用选择程序.请帮我
发布于 2022-06-14 09:32:06
正如您可以从注释中看到的,解决方案是部分的,但是您可以搜索正在运行的进程中是否有与所选文件同名的进程。
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
}
}https://stackoverflow.com/questions/72613401
复制相似问题