File associations & executables

M

Mark Rae

Hi,

I have written an MDI Windows forms app in C# which, among other things,
maintains data in XML files with a specific extension. I know how to
associate files of that extension with my app by using the Open With
functionality in Windows explorer - now if I click on one of the files, it
launches my app. However, how do I then get my app to open the file? In VB
classic, I used to use the Command$ method for this, but don't know what the
C# equivalent is.

Secondly, I have prevented multiple instances of my app from running with
the following code:

if
(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.Ge
tCurrentProcess().ProcessName).GetUpperBound(0) > 0)
{
MessageBox.Show("Test Application is already running", "Test
Application", MessageBoxButtons.OK, MessageBoxIcon.Stop);
this.Close();
return;
}

However, I still want users to be able to click on a file to launch the app
and open the file, then click on another file which will also open the file
but not try to launch a second instance of the app, exactly the same as
clicking on, say, more than one Excel .xls file or more than one Word .doc
file would.

Is this possible?

Any assistance gratefully received.

Best regards,

Mark Rae
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

Check the args parameter to your entry method (typically, it is Main).
This array will contain the parameters that are passed to your executable,
in this case, it should be the name of the file that was double clicked.

What you want to do is possible, but will require a good deal of work.
First, I wouldn't check the process name for determining if your application
is running. I would create a named mutex and check to see if you can
acquire it. If you can, then great, if not, then don't let the application
run.

To have another file opened in an existing open application, I would
recommend that you create a remotable type in your application. Your
application would use a well known location to publish itself. When your
application is started, try to acquire the mutex. If the mutex can not be
acquired, then check to see if a file name was passed in. If it was, then
you get the remoted object your running application is hosting, and then
send the filename (through a method or property) to your app. Then, your
app will receive the name of the file, and open it up.

While you could check for the existence of the remoting server as a
check to see if another instance of the app is running, it is probably less
performant than trying to acquire the mutex, which is why I left it in.

Hope this helps.
 
M

Mark Rae

message
Nicholas,
Check the args parameter to your entry method (typically, it is Main).
This array will contain the parameters that are passed to your executable,
in this case, it should be the name of the file that was double clicked.
Excellent!

What you want to do is possible, but will require a good deal of work.

I had a feeling it might...
First, I wouldn't check the process name for determining if your application
is running.

Oh, OK. I got that from one of the C# developers' sites. I did a Google
search for something like "C# App.PrevInstance equivalent" and this seemed
like a good suggestion. Works perfectly, by the way, but I wouldn't know how
"performant" (is that *really* a word?) it is...
I would create a named mutex and check to see if you can
acquire it. If you can, then great, if not, then don't let the application
run.

To have another file opened in an existing open application, I would
recommend that you create a remotable type in your application. Your
application would use a well known location to publish itself. When your
application is started, try to acquire the mutex. If the mutex can not be
acquired, then check to see if a file name was passed in. If it was, then
you get the remoted object your running application is hosting, and then
send the filename (through a method or property) to your app. Then, your
app will receive the name of the file, and open it up.

While you could check for the existence of the remoting server as a
check to see if another instance of the app is running, it is probably less
performant than trying to acquire the mutex, which is why I left it in.

Hope this helps.

Hmm - does anyone have any examples of this...?

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top