Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I do double-click file opening with my C# app?

How do I do double-click file opening with my C# app?

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpdata-structures
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spazzman
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • S spazzman

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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

        S Offline
        S Offline
        spazzman
        wrote on last edited by
        #3

        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?

        M D 2 Replies Last reply
        0
        • S spazzman

          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?

          M Offline
          M Offline
          MilesAhead
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S spazzman

            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?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            S 1 Reply Last reply
            0
            • D Dave Kreskowiak

              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

              S Offline
              S Offline
              spazzman
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups