Reputation: 79
In my VB.net application I am opening a PDF file using System.Diagnostics.Process.Start("c:\TEMP\MyFile.pdf").
Is it possible to Close this file Programatically in some event.
Upvotes: 0
Views: 9504
Reputation: 823
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.
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
Upvotes: 1
Reputation: 3653
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:
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!)
You may also use the Process.HasExited
property to see if the PDF has been closed, and may process your code based on that.
Upvotes: 3
Reputation: 350
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.
If you are having permission problems. Use the AppData
folder. It has the necessary permissions that programs need to run
Upvotes: 0
Reputation: 1
This is in C# but may come in handy...
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);
}
Upvotes: 0