How do I do double-click file opening with my C# app?
-
I've written an app in C# - let's call it Foo. Foo creates *.Bar files. I want to enable double click support in Windows for *.Bar files, so Windows automatically opens the file in a running instance of Foo. I already know how to get Windows to start Foo up when I double click on a .Bar file - simply associate the file type using Windows Explorer (for example), and use the args[] string array argument in the Main(args[]) method of Foo. This acts as a list of files to open when Foo starts up. But what happens when Foo is _already_ running? What pathway does Windows use to send an open-file instruction to Foo? Does it go through the Main() method? I've written Foo so only one instance of it will run at a time, so I need to be able to get Windows to tell the existing instance of Foo to open a file. Any ideas how this is done? Thanks
-
I've written an app in C# - let's call it Foo. Foo creates *.Bar files. I want to enable double click support in Windows for *.Bar files, so Windows automatically opens the file in a running instance of Foo. I already know how to get Windows to start Foo up when I double click on a .Bar file - simply associate the file type using Windows Explorer (for example), and use the args[] string array argument in the Main(args[]) method of Foo. This acts as a list of files to open when Foo starts up. But what happens when Foo is _already_ running? What pathway does Windows use to send an open-file instruction to Foo? Does it go through the Main() method? I've written Foo so only one instance of it will run at a time, so I need to be able to get Windows to tell the existing instance of Foo to open a file. Any ideas how this is done? Thanks
Windows doesn't do the communication for you. Your app has to find the previous instance that is running and send the commandline, or whatever you need, to that previous instance. Check out these articles here on CodeProject: http://www.codeproject.com/vb/net/singleinstance.asp[^] http://www.codeproject.com/csharp/restricting_instances.asp[^] http://www.codeproject.com/csharp/singleinstcs.asp[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Windows doesn't do the communication for you. Your app has to find the previous instance that is running and send the commandline, or whatever you need, to that previous instance. Check out these articles here on CodeProject: http://www.codeproject.com/vb/net/singleinstance.asp[^] http://www.codeproject.com/csharp/restricting_instances.asp[^] http://www.codeproject.com/csharp/singleinstcs.asp[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
ok, but how do I get the previous instance then? These examples are all for preventing multiple instances. I think the 2nd one has some useful code that allows me to get the process object for the previous instance, but I can't seen to get the actual instance of the application (a winform object) from that process object, so I'm still kinda stuck. Any ideas?
-
ok, but how do I get the previous instance then? These examples are all for preventing multiple instances. I think the 2nd one has some useful code that allows me to get the process object for the previous instance, but I can't seen to get the actual instance of the application (a winform object) from that process object, so I'm still kinda stuck. Any ideas?
There are several approaches you can take to transmit the command line info(such as the filename) from the secondary instance of your app to the primary. You can use sockets, windows messaging, shared memory etc.. I did an ActiveX Control that uses a page of the system swap file with a memory mapped file to do it. It works with just about everything I've tried that can understand ActiveX. If you can read Delphi, the source to the component that the ActiveX Control is derived from is online. http://www.torry.net Search on "TellTail" to download the Delphi source. Basically it uses a named mutex to determine if you're in the primary or secondary instance of the app. If in the secondary, it copies command line info to the shared memory page. The primary instance has to periodically check if any info has been put in shared memory then call a method to copy it out. I'm reworking it now to use the 'Global\' namespace if in XP for the named kernel objects but I may also change it to use a system event to eliminate the polling in the primary app. When the next revision comes out I'll have to decide how I'm going to distribute it. But for now the Delphi source might give you some ideas how to do it in C#. Good luck.
-
ok, but how do I get the previous instance then? These examples are all for preventing multiple instances. I think the 2nd one has some useful code that allows me to get the process object for the previous instance, but I can't seen to get the actual instance of the application (a winform object) from that process object, so I'm still kinda stuck. Any ideas?
The first article I gave you does exactly what your talking about. All you have to do is examine the source code. There is also an article[^] on MSDN that demonstrates what you want to accomplish. There is a follow up article for that one that fixes a bug in the first article here[^]. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
The first article I gave you does exactly what your talking about. All you have to do is examine the source code. There is also an article[^] on MSDN that demonstrates what you want to accomplish. There is a follow up article for that one that fixes a bug in the first article here[^]. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Sorry, I guess I wasn't being clear enough. Yeah, that article was doing what I wanted. The solution just seemed overly complex, and it was in VB (I'm using C#). Plus I'm writing an open source application, so I have to be careful what code I compile into my app. At the time I didn't realise it would be so much work to have one instance of an application contact another - I figured it was just a case of getting the instance via its handle, which I now understand is not enough. Thanks for your help ... guess I'll be taking a closer look at that VB code after all.