Click here to Skip to main content
15,909,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have a c# application which loads xml files from a specified directory and displays their content through dynamically created tab pages.

I want to know if there is a way to load an xml file directly into my application by right clicking on the xml file, choosing openwith, and selecting my application.

Any help would be highly appreciated. Ask for further explanation if need be.

Thank you
Posted
Updated 24-Sep-10 1:27am
v3
Comments
OriginalGriff 24-Sep-10 7:53am    
Don't post comments to answers as a new answer - nobody but you gets notified. Use "Add Comment" on the answer you want to respond to instead.

The best way is top set the association when you install it, but otherwise you have to play with the Registry:
C#
[System.Runtime.InteropServices.DllImport("shell32.dll")]
static extern void SHChangeNotify(int wEventId, int uFlags, int dwItem1, int dwItem2);
/// <summary>
/// Associate a file extension with an application
/// </summary>
/// <param name="extension">The extension to be registered (eg ".CAD")</param>
/// <param name="className">The name of the associated class (eg "CADDoc")</param>
/// <param name="description">The textual description (eg "CAD Document")</param>
/// <param name="exeProgram">The app that manages that extension (eg "c:\Cad\MyCad.exe")</param>
/// <returns></returns>
private bool CreateFileAssociation(string extension, string className, string description, string exeProgram)
    {
    const int SHCNE_ASSOCCHANGED = 0x8000000;
    const int SHCNF_IDLIST = 0;
    // Ensure that there is a leading extension indicator - full stop
    if (extension.Substring(0, 1) != ".")
        {
        extension = "." + extension;
        }
    Microsoft.Win32.RegistryKey key1 = null;
    Microsoft.Win32.RegistryKey key2 = null;
    Microsoft.Win32.RegistryKey key3 = null;
    try
        {
        // create a value for this key that contains the classname
        key1 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(extension);
        key1.SetValue("", className);
        // create a new key for the Class name
        key2 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(className);
        key2.SetValue("", description);
        // associate the program to open the files with this extension
        key3 = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(className + @"\Shell\Open\Command");
        key3.SetValue("", exeProgram + @" ""%1""");
        }
    catch
        {
        return false;
        }
    finally
        {
        if (key2 != null)
            {
            key2.Close();
            }
        if (key3 != null)
            {
            key3.Close();
            }
        }
    // Notify Windows that file associations have changed
    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
    return true;
    }
 
Share this answer
 
v2
Comments
OriginalGriff 24-Sep-10 7:52am    
The OP wrote: "Thank you for your answers. I'll try them both and return to you with my feedback."
deadwood88 24-Sep-10 8:13am    
Is there another way that doesn't involve file association?
If they are your generated and controlled xmls (maybe from another tool or something) I recomend that you give it a different extension.

e.g. *.mySweetApp

Then use the File Types editor for the installers and associate your file to the application. This also will allow you to put an icon on those files.


With the File Type editor you can tell the application to start by the file being clicked (or other actions for that matter). The application will start and pass the file name as an argument. You can then open it as need be. You will need an IF check on your args count.

e.g.
if(args.Length > 0)
{
   StartAppWithThisFile(args[0]);
}
else
{
   StartAppAsNormal());
}


If you do it this way you can avoid all XML files being tied to your application as numerous applications use XML. The file is still of course XML but you are just giving it a unique tag.
 
Share this answer
 
v2
Comments
deadwood88 24-Sep-10 9:51am    
Thank you for your input, however i dont want to involve any file association with my application.
Goutam Patras' answer is satisfactory for me.
Environment.CommandLine will give you the commandline Args. You can process the commandline parameter and get the file name and process it.
 
Share this answer
 
Comments
OriginalGriff 24-Sep-10 7:52am    
The OP wrote: "Thank you for your answers. I'll try them both and return to you with my feedback."
deadwood88 24-Sep-10 8:22am    
I want to load the application by clicking [OpenWith] on a certain XML file and selecting my application.
How can I use or pass parameters in such a scenario?
Goutam Patra 24-Sep-10 8:49am    
You dont need to pass a parameter. "Load the application by clicking [OpenWith]" display the "Environment.CommandLine" using a message box and your find the answer yourself.
deadwood88 24-Sep-10 8:57am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
deadwood88 24-Sep-10 8:57am    
Thank you! Exactly what i need.

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