File Association in VB .NET

Discussion in 'Programming' started by Schadenfroh, Jun 27, 2007.

  1. Schadenfroh

    Schadenfroh Elite Member

    Joined:
    Mar 8, 2003
    Messages:
    38,418
    Likes Received:
    0
    I have been playing around with the code here, it was mainly written for startup only , but the one I am currently working on only needs one instance open at a time and I wish it to be able to read-in text files of a current extension.

    I have already conquered associating a file type with my program and getting it to read in it's text content on startup, but how would I go about getting it to read in if the program is already open? I theorized using a background worker with similar code to the one that I linked and the results were.... not pretty.

    Anyone know of a better way?

    Thanks

    Update: I gave up and I am porting it all to C#
     
  2. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,683
    Likes Received:
    3
    Hmm, well, it's Explorer.exe that launches the associated application and passes in the target file description. In the advanced section of the file associations dialog you can tell explorer to use DDE to notify a running application of a file launch. The application would have to be written to handle the DDE events, and I don't know how that is done in .Net, never having messed with it. It's probably handled similarly to file drop events.

    Otherwise, explorer will attempt to launch a new instance of the associated application. What you could do is have that application check for a previously running instance on startup (you can use FindWindow, or one of the process APIs), before it draws its own window. Of course, you would still need to communicate between the processes, so you're into DDE or pipes or message insertion or something anyway, but at least the behavior wouldn't depend on having set up the file association correctly.
     
  3. Schadenfroh

    Schadenfroh Elite Member

    Joined:
    Mar 8, 2003
    Messages:
    38,418
    Likes Received:
    0
    Interesting idea, Mark. I have thought about launching a 2nd application and figuring out a way to transfer the data if I cannot find a less complex way. I have done much searching and cannot find an easier way to do it:(

    Any VB .NET gurus able to offer any ideas?
     
  4. Noobsa44

    Noobsa44 Member

    Joined:
    Jun 7, 2005
    Messages:
    65
    Likes Received:
    0
    You don't say if you're using .net 1.1 or 2.0, which does make a difference.

    In .net 2.0 (using visual studios) there is a check box (under "Application") to sent your program to Single Instance. This isn't a perfect solution, as if you try to open multiple instances before the first instance is up, it will cause the program to crash. Also, this requires you to do some fancy work to pass the arguements after the program has started.

    In .net 1.x you have to use a mutex and (if I recall right) use the windows API to send data back and forth. For more on that, check this page out: http://www.codeproject.com/vb/net/singleinstance.asp

    I happen to have examples of both methods, as I have used both for my open source media player, called GAmp. For the .net 2.0 method, look at my GAmp 2.0 release, and for the .net 1.x method, look at my 1.0 release.