Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi people, I've developed a C# win application which works with some filetypes. I've associated a specified filetype through an installshield setup project. Everything is OK: After setup, the filetype becomes associated with the application and user can run the application with double-clicking an associated file. Also, I've developed my application compatible with "Drag & Drop" file which works nice.
...When I double click an associated file, the application runs successfully, and I expect that the file will be opened in the application, but no file is opened automatically. Seems that an Event must handle this, but which Event? Have anyone faced this problem?

Thanks you two JF2015 & SAKryukov for the answer.
Let me explain more:
My application starts with a mdi-parent form 'MainForm', with 'AllowDrop' property switched to true.

First: Drag Drop Definition:
private void MainForm_DragEnter(object sender, DragEventArgs e)
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy;

        e.Effect = DragDropEffects.None;

private void MainForm_DragDrop(object sender, DragEventArgs e)
    Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
    string fn = a.GetValue(0).ToString();
    if (fn.IndexOfAny(new char[]{'.'}) == -1)
        RadMessageBox.Show("Cannot open " + fn + ". Make sure it's a supported file.", "Error", MessageBoxButtons.OK,RadMessageIcon.Error);
    if (fn.Substring(fn.LastIndexOf('.'), 4).ToUpper() != ".MGP")
        RadMessageBox.Show("Cannot open " + fn + ". Make sure it's a supported file.", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);

private bool OpenFile(string fn)
    //Some Code to open the file

After a successful drag-drop a file in mainform, it's opened as a mdichild.

The question is How could I open this file as an mdichild when I double-click it?
Updated 30-Jan-11 20:43pm
Sergey Alexandrovich Kryukov 31-Jan-11 0:54am    
Which drag-and-drop?
Alireza Hajihasani 31-Jan-11 2:32am    
Question Improved, See above.
Sergey Alexandrovich Kryukov 31-Jan-11 0:55am    
Do you process Command Line at all?
Alireza Hajihasani 31-Jan-11 2:32am    
No, I haven't processed command line yet. May you explain how to do?
Sergey Alexandrovich Kryukov 31-Jan-11 2:55am    
OK, I see. My answer is correct now. Read my article: it is comprehensive. If you don't want to use my method and library, you will know what to do.

1 solution

I suspect you don't process command line.
(for a robust command line utility, you may want to see my work: Enumeration-based Command Line Utility[^].)

Please forgive me if this is not an answer: you did not explain your problem in full.
Here is what I suspected and why: you develop drag-and-drop as some control feature, which works when your UI is already shown. You did not explain this exactly, because the Shell also has its drag-and-drop functionality. You put registration of your application in your installation. What's the missing link? Command line, I don't know what else. You never said you processed it.

No events involved, this is simpler than that.

After OP confirmation, I know the problem is command line.
Consider the application's entry assembly has a main (start-up) module in the file application.exe, and the system Registry has the file extension registration records which show system Shell to use this application, with full path name of this file. Let's assume this is an extension ".myext". In this case, when a file, for example "mydata.myext" is clicked on any file manager using the Shell, it created a process using the following command line:

application.exe mydata.myext
(with full path name оf application.exe.)

During run-time, this command line will be passed as a parameter to applications entry point, such as function main, it also can be retrieved using the property System.Environment.CommandLine. The application should be responsible for finding the file (if a parameter is a file, like in your case) and processing it.

Much more difficult case if the application is already running, and you need to load a file in first instance. This is all about single-instance behavior, which can only be implemented by the application itself. The explanation of the required machinery could be a matter of a while article, may be a big article. Here is the schematic description:

1) Instance of your application #1 is running;
2) You click in your file with registered extension — instance #2 is started;
3) instance #2 detected that instance #1 is running;
4) instance #2 sends it command line data to instance #1;
5) when command line data is transmitted, instance #2 terminate itself;
6) when command line data is transmitted, instance #1 peforms processing of the command line (locate and load the file(s), etc.)
The best way to communicate between instances in .NET is remoting (based on named pipes, because the instances are always on the same computer. The detection of the already running instance is best implemented using the same thing: if instance #1 does not accept connection, it means it is not running.

That's it, a complete answer. If you need more detail, please ask concrete questions, otherwise formulate a separate question on a new page.

Good luck.

Share this answer
JF2015 31-Jan-11 1:02am    
I too suspect that this is his problem. Hard to guess when the description of what he's doing is missing. 5 for the hints you gave the OP.
Sergey Alexandrovich Kryukov 31-Jan-11 1:04am    
Thank you, JF2015; you and let's see what OP says. (Many never comes back, especially if you ask them questions showing that a question is not yet formulated. This case should be easy, I hope)
Sergey Alexandrovich Kryukov 31-Jan-11 3:17am    
OP confirmed, please see complete answer.
JF2015 31-Jan-11 3:20am    
Good, but I can only vote once ;)
Sergey Alexandrovich Kryukov 31-Jan-11 3:25am    
Thank you, this is no more but just follow-up information, because you don't receive notification on Answer update (or do you :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900