Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# CF: How to open specific file extension with my program

I'm developing a mobile application in .Net Compact Framework. I managed to edit the registry HKEY_CLASSES_ROOT so that a click on a file with the .xyz extension will open my application. Basically, I need to do some operation on this file when it's clicked.

However, I realise that if I do that the first time, it reaches program.cs at static void Main. But when the program is running and I click on the file with .xyz extension again, it doesn't load the program static void Main. I tried setting breakpoints at the form that is currently running but still nothing.

So where does it go to? How can I detect file .xyz is clicked and do something?

like image 231
chrizonline Avatar asked Mar 12 '10 22:03

chrizonline


2 Answers

The big problem you're having is that once the application is running Main will never get called again, and in fact it shouldn't.

Under Windows Mobile, unfortunately, the CF itself attempts to keep applications singletons, so when you try to runt he app a second time, the CLR itself intercepts that and instead brings the existing instance to the fore. One of the unfortunate side effects of this is that you get no opportunity to handle command-line parameters.

To get this to work, you have to do a few things:

  1. Subvert the CF's attempt to make your app a sinlgeton so you can run multiple instances.
  2. Hold a mutex in the app to know if you are already running.
  3. Spawn a thread early on to listen for incoming parameters from subsequent instances. When parameters come in, bring yourself to the fore and handle the parameters appropriately.
  4. On startup, if the mutex already is taken (i.e. the app is already running), use IPC (a point-to-point queue is a good way) to send the existing app's listener thread (see #3) your parameters and then just quit.
like image 114
ctacke Avatar answered Oct 12 '22 13:10

ctacke


You say that you need to perform some operation on a file every time it's clicked. I'm assuming this is something GUI-related, like you want to show properties of the file when it's double-clicked.

Assuming your program has one main form, you could set its MinimizeBox property to false and in its Deactivate event put this.Close();. This way, when you click a file of the right type, your application will start and read the command line args and display the file details. If the user then closes your app with the OK button in the upper right, it will close out for real so that the next time it opens it will open a new instance and read the command line args properly. Or, if the user just navigates to some other program in WinMo, your application's Deactivate event will fire, closing the app. Either way, the app is always either open and on top, or completely closed, so clicking a file in file explorer will always open a new instance.

like image 43
MusiGenesis Avatar answered Oct 12 '22 13:10

MusiGenesis