有没有办法以编程方式在 VB.net 中关闭打开的 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15976334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:14:36  来源:igfitidea点击:

Is there a way to close an open PDF file in VB.net programatically

vb.net

提问by sanika

In my VB.net application I am opening a PDF file using System.Diagnostics.Process.Start("c:\TEMP\MyFile.pdf").

在我的 VB.net 应用程序中,我使用 System.Diagnostics.Process.Start("c:\TEMP\MyFile.pdf") 打开一个 PDF 文件。

Is it possible to Close this file Programatically in some event.

是否可以在某些情况下以编程方式关闭此文件。

回答by Prahlad Yeri

Yes, there is one way, though it is not a very elegant solution.

是的,有一种方法,尽管它不是一个非常优雅的解决方案。

When you start the PDF process, you capture the process-id in some global variable:

当您启动 PDF 进程时,您会在某个全局变量中捕获进程 ID:

Dim id As Integer 'Global variable
id = System.Diagnostics.Process.Start("C:\Temp\myfile.pdf").Id

Then, when you need to kill the process, just do:

然后,当您需要终止进程时,只需执行以下操作:

System.Diagnostics.Process.GetProcessById(id).Kill()

(make sure that there is a process with this id that is actually running!)

(确保有一个具有此 ID 的进程正在实际运行!)

You may also use the Process.HasExitedproperty to see if the PDF has been closed, and may process your code based on that.

您还可以使用该Process.HasExited属性来查看 PDF 是否已关闭,并可以基于此处理您的代码。

回答by Ateeq

I don't think that it's possible to close a specific PDF file because they are not an independent process, they are sub processes in the task manager. you can kill the adobe acrobat reader process it self.

我认为关闭特定的 PDF 文件是不可能的,因为它们不是一个独立的进程,它们是任务管理器中的子进程。您可以自行终止 adobe acrobat reader 进程。

Dim AcrobateInstance() As Process = Process.GetProcessesByName("AcroRd32")
If AcrobateInstance.Length > 0 Then
   For value As Integer = 0 To AcrobateInstance.Length - 1
      BillInstance(value).Kill()
   Next
End If

回答by Imran B

This is in C# but may come in handy...

这是在 C# 中,但可能会派上用场......

var myPDFEvent = System.Diagnostics.Process.Start(@"C:\Temp\myfile.pdf");myPDFEvent.Exited += new EventHandler(myPDFEvent_Exited);myPDFEvent.EnableRaisingEvents = true;

var myPDFEvent = System.Diagnostics.Process.Start(@"C:\Temp\myfile.pdf");myPDFEvent.Exited += new EventHandler(myPDFEvent_Exited);myPDFEvent.EnableRaisingEvents = true;

void myPDFEvent_Exited(object sender, EventArgs e){System.IO.File.Delete(@"C:\Temp\myfile.pdf);}

void myPDFEvent_Exited(object sender, EventArgs e){System.IO.File.Delete(@"C:\Temp\myfile.pdf);}

回答by Kraxed

This might work:

这可能有效:

 Process1.Start()
 Process1.WaitForExit()
 If Process1.HasExited Then
     System.IO.File.Delete(Your File Path)
 End If

Make sure to add the Process Object onto the form from the toolbox and configure the startinfo section.

确保将 Process Object 从工具箱添加到表单中并配置 startinfo 部分。

If you are having permission problems. Use the AppDatafolder. It has the necessary permissions that programs need to run

如果您遇到权限问题。使用AppData文件夹。它具有程序运行所需的必要权限