Search code examples
c#winformsacrobat

How to identify and close an instance of opened application in C#


I just load a PDF file in a WinForm using WebBrowser control. I have a PDF byte array and I used to write this in temporary folder of client machine when the user clicks on print preview in my application.

After preview when the user closes the form I need to delete the file. Before deleting I need to close the instance of Adobe Reader that was opened for preview.

I tried getting a list of processes using

Process[] processes = Process.GetProcessesByName("AcroRd32");

I can kill the process by this means and delete the file from temporary folder. Here comes the actual problem. Suppose the user already opened a PDF file for his own purpose then by finding the running instances and by closing them I will close his reader too.

Is there any way to find the instance of Adobe reader which is opened by my application. I need to check for Adobe reader only. Not any other.

UPDATE

I have opened adbobe reader manually which has filename or MainWindowTitle as Magazine.pdf first.Then i opened adobe reader by webbrowser control in my application.But it has MainWindowTitle as Empty.Also there shoul be two intance of adobe reader but in taskmanager it shows four instance and of them only one has MainWindowTitle as Magazine.pdf See the below images for reference

Four Process of reader

Last process with MainWindowTitle


Solution

  • Check the MainWindowTitle of each Acrobat process - the one you want will begin with the name of the temp file.