Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have files with my own special file type which I want to open in my own app by double clicking the file name in windows explorer.
That works fine: The app gets the file name as an argument to the Main()-program.

My problem: When I try to open a second file a new instance of my app opens. That's not what I want. I rather want that the the second file opens in the instance already running (like for example the standard behavior of the acrobat reader).
Any hints how to implement that?

What I have tried:

I tried to find a solution on the net but without success.
Posted
Updated 1-Oct-19 6:35am

That's complicated, because Windows isn't set up to do that for you - apps should accept a command line argument, but once they are running you can't add to or otherwise change that collection.

So your app has to be "aware": when it starts, it checks for and existing instance (which is pretty easy: A simple way to ensure only one instance is running.[^] should help) and if one is, then talk to it and transfer the file data.
A Socket in a separate task permanently listening for connections should do it.
 
Share this answer
 
Comments
Member 13566383 1-Oct-19 4:11am    
Thank you for your advice. To make sure that only one instance is running was an easy task.
But I'm afraid my first real encounter with sockets will turn out a little bit more challenging. But I will try.
OriginalGriff 1-Oct-19 4:24am    
You're welcome - there are plenty of simple examples of "how to do Sockets" available, both on CP:
https://www.codeproject.com/search.aspx?q=basic+sockets&x=0&y=0&sbo=kw
and Google:
https://www.google.com/search?q=basic+Sockets+c%23&oq=basic+Sockets+c%23&aqs=chrome..69i57j0l2.7639j0j7&sourceid=chrome&ie=UTF-8

They aren't too difficult, honestly! Just remember that the "listener" needs to be in a separate thread so you don't block the main thread, and that you'll probably want to Invoke at some point to get new info back to the main thread so you can access controls.
Just to make your life simpler ... I created a class to make it all rather easier: Double clicking a file in explorer and adding it to your app while it's running.[^]

It does exactly what I said to do in the original answer.
 
Share this answer
 
Comments
Member 13566383 1-Oct-19 15:27pm    
It's already running. Did require little more than copy and paste.
Thank you very much again.
OriginalGriff 1-Oct-19 15:37pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900