C#:杀死打开的特定文件而不是所有实例

发布于 2025-01-15 00:14:02 字数 577 浏览 5 评论 0原文

我正在尝试通过 Process 在我的应用程序中打开 Excel 文件。 当表单关闭时,我想自动关闭该文件。 我面临的问题是,即使我指定打开的 Excel 的 PID 进程。 当我运行kill指令时,所有其他打开的excel文件都将关闭,而不是我需要的特定文件。

请你帮助我好吗 。

 File.WriteAllBytes(filePath, bytes);
 proc = new Process();
 proc = Process.Start(filePath);
 processId = proc.Id; //keep PID to kill that process 

  proc.WaitForInputIdle(); //Set focus on opened file
  SetForegroundWindow(this.Handle);                        
  proc.Dispose();
// killing the process after file use
  Process processes = Process.GetProcessById(processId);
  processes.Kill();

I'm trying to open an excel file in my application via Process.
When the form is closed I want to close that file automatically .
The issue that I'm facing is even if I specify the PID process of the Excel opened .
When I run the kill instruction, all the other excels file opened will close and not specifically the one that I need.

Could you please help me .

 File.WriteAllBytes(filePath, bytes);
 proc = new Process();
 proc = Process.Start(filePath);
 processId = proc.Id; //keep PID to kill that process 

  proc.WaitForInputIdle(); //Set focus on opened file
  SetForegroundWindow(this.Handle);                        
  proc.Dispose();
// killing the process after file use
  Process processes = Process.GetProcessById(processId);
  processes.Kill();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文