locked
How I can create a file association? RRS feed

  • Question

  • how i can associate a file type with my application? ps: I wan't somethink easy..
    Tuesday, May 30, 2006 5:13 PM

Answers

  • Lucky for you this has been a bit of a lazy afternoon and because of that I built you a quick managed class to do this for you:

        class FileExtension

        {

            public FileExtension(string applicationName)

            {

                this.applicationName = applicationName;

                this.fileTypeName = applicationName + " file";

            }

     

            public FileExtension(string applicationName, string fileTypeName)

            {

                this.applicationName = applicationName;

                this.fileTypeName = fileTypeName;

            }

     

            private string fileTypeName;

     

            public string FileTypeName

            {

                get { return fileTypeName; }

                set { fileTypeName = value; }

            }

     

            private string applicationName;

     

            public string ApplicationName

            {

                get { return applicationName; }

                set { applicationName = value; }

            }

     

            public void SetExtension(string extension)

            {

                RegistryKey root = Registry.ClassesRoot;

                RegistryKey key;

     

                //Create extension key that refers to application name

                key = root.CreateSubKey(extension);

                key.SetValue("", applicationName, RegistryValueKind.String);

                key.Close();

     

                //Create application key that is referenced by extension key

                key = root.CreateSubKey(applicationName);

                key.SetValue("", fileTypeName, RegistryValueKind.String);

     

                //Create open command for application type (enables multiple files to use same application key)

                key = key.CreateSubKey(@"shell\open\command");

                key.SetValue("", System.Windows.Forms.Application.ExecutablePath + " %1", RegistryValueKind.String);

                key.Close();

               

                root.Close();

            }

     

            public bool Verify(string extension)

            {

                RegistryKey root = Registry.ClassesRoot;

                RegistryKey key;

     

                key = root.OpenSubKey(extension);

     

                //If extension key does not exist, return false

                if (key == null)

                {

                    return false;

                }

     

                string appName = (string)key.GetValue("");

     

                //If configured application name does not match expected, return false

                if (appName != applicationName)

                {

                    return false;

                }

               

                //Assume that application references this one

                return true;

            }

     

        }

    In order to use it you will need to create an instance of the class and pass in a name for your application and possibly a file type descriptor such as “Media file” or “Such and such data file”.

    After that simply call SetExtension() while passing in the extension you want to want to set (in .xxx format) and when it returns it should be added (provided you have rights to access the key in question (I have added no error detection/prevention to this code)).

    On startup of your app later, call Verify() while passing in your extension to see if it has been taken over by another app, if it has then you may want to call SetExtension() again or prompt the user for permission to do so first.

    Tuesday, May 30, 2006 8:08 PM

All replies

  • Amazingly I have been unable to find a piece of sample code on how to do this in C# in managed code... instead, try this old school and trusty VB6 code from Microsoft that provides the basic steps required.
    Tuesday, May 30, 2006 6:47 PM
  • Lucky for you this has been a bit of a lazy afternoon and because of that I built you a quick managed class to do this for you:

        class FileExtension

        {

            public FileExtension(string applicationName)

            {

                this.applicationName = applicationName;

                this.fileTypeName = applicationName + " file";

            }

     

            public FileExtension(string applicationName, string fileTypeName)

            {

                this.applicationName = applicationName;

                this.fileTypeName = fileTypeName;

            }

     

            private string fileTypeName;

     

            public string FileTypeName

            {

                get { return fileTypeName; }

                set { fileTypeName = value; }

            }

     

            private string applicationName;

     

            public string ApplicationName

            {

                get { return applicationName; }

                set { applicationName = value; }

            }

     

            public void SetExtension(string extension)

            {

                RegistryKey root = Registry.ClassesRoot;

                RegistryKey key;

     

                //Create extension key that refers to application name

                key = root.CreateSubKey(extension);

                key.SetValue("", applicationName, RegistryValueKind.String);

                key.Close();

     

                //Create application key that is referenced by extension key

                key = root.CreateSubKey(applicationName);

                key.SetValue("", fileTypeName, RegistryValueKind.String);

     

                //Create open command for application type (enables multiple files to use same application key)

                key = key.CreateSubKey(@"shell\open\command");

                key.SetValue("", System.Windows.Forms.Application.ExecutablePath + " %1", RegistryValueKind.String);

                key.Close();

               

                root.Close();

            }

     

            public bool Verify(string extension)

            {

                RegistryKey root = Registry.ClassesRoot;

                RegistryKey key;

     

                key = root.OpenSubKey(extension);

     

                //If extension key does not exist, return false

                if (key == null)

                {

                    return false;

                }

     

                string appName = (string)key.GetValue("");

     

                //If configured application name does not match expected, return false

                if (appName != applicationName)

                {

                    return false;

                }

               

                //Assume that application references this one

                return true;

            }

     

        }

    In order to use it you will need to create an instance of the class and pass in a name for your application and possibly a file type descriptor such as “Media file” or “Such and such data file”.

    After that simply call SetExtension() while passing in the extension you want to want to set (in .xxx format) and when it returns it should be added (provided you have rights to access the key in question (I have added no error detection/prevention to this code)).

    On startup of your app later, call Verify() while passing in your extension to see if it has been taken over by another app, if it has then you may want to call SetExtension() again or prompt the user for permission to do so first.

    Tuesday, May 30, 2006 8:08 PM
  • Thanks.The code is working but were I put the tipe of extension?For example : .MP3 and .WAV
    Wednesday, May 31, 2006 5:07 PM
  • Put the tipe? Do you mean type name? If so, it is the second argument to the constructor and to use the whole thing you’d simply do something like this:

                FileExtension fe = new FileExtension("MyMediaApp", "My Media App Media File");

                fe.SetExtension(".mp3");

                fe.SetExtension(".wav");


    With this you are creating an instance of the class and asking that it set the extension for .mp3 and .wav to the application name (can be anything you want) specified in the constructor as well as the type name of the file.

    Wednesday, May 31, 2006 5:46 PM
  • Nop...is not working :-((
    Wednesday, May 31, 2006 7:16 PM
  • In what way does it not work?
    Wednesday, May 31, 2006 7:20 PM
  • Well....I put the code in my application (at the begining), but a .MP3 file is stile registred to Windows Media Player
    Thursday, June 1, 2006 7:19 AM