0

I am opening a PDF file using Process myProcess = Process.Start(fileLocation);. I need to close this PDF file before opening a new file. Otherwise I will end up with hundreds of instances open on my default PDF reader.

I attempted to use myProcess.CloseMainWindow(); but I got an error that the window had already closed. I think myProcess works by creating a secondary Process that displays the pdf. I have no information about this potential secondary process.

I am currently using MicrosoftEdge as my default pdf application and coding in Microsoft Framework 4.8.

CBFT
  • 273
  • 2
  • 14
  • You might find a hack for edge (i somewhat doubt that but might be possible) but in the common case you don't know the app handling pdf so you can't predict the concret behavior and you better not try to close anything. People will hate you if you close their complete browser with all there other tabs. – Ralf Feb 06 '23 at 18:16
  • You must expect that the user has an active session using the browser or PDF viewer n general. In other words, your document might not be the only open document in the viewer. Most viewers like web browsers support tabbed view where multiple documents are open. It would be fatal to shutdown the complete application. Even if you explicitly open a new browser instance, you don't know if the user uses it to continue browsing. – BionicCode Feb 06 '23 at 19:44
  • 2
    Depending on your application you can a) simply open it and let the user handle it b) ask the user if you should open it again c) check if the document is already open and activate the hosting application (move window to the foreground) d) use an embedded viewer that you can control conveniently – BionicCode Feb 06 '23 at 19:44
  • You might check if NuGet package [WebView2](https://learn.microsoft.com/en-us/microsoft-edge/webview2/) meets your needs. – Tu deschizi eu inchid Feb 07 '23 at 03:28
  • I tried using WebView2. My plugins load via reflection. That does not work with webview.EnsureCoreWebView2Async. In most cases I am opening the document to different hyperlinks so checking if it is already open doesn't work. I guess my best option is to find a workaround for WebView2. – CBFT Feb 08 '23 at 15:30
  • You haven't provided any code regarding WebView2, but it's important to ensure it's properly initialized. The following (in VB.NET) may be of interest https://stackoverflow.com/a/73846538/10024425 – Tu deschizi eu inchid Feb 08 '23 at 15:41

0 Answers0