How I can create a file association?

how i can associate a file type with my application ps: I wan't somethink easy..


Answer this question

How I can create a file association?

  • JoeBlow

    In what way does it not work

  • Ionela

    Thanks.The code is working but were I put the tipe of extension For example : .MP3 and .WAV

  • Dracoman

    Nop...is not working :-((

  • display name you have entered

    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.



  • Zodzetrick

    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.



  • Allam

    Well....I put the code in my application (at the begining), but a .MP3 file is stile registred to Windows Media Player

  • Krismu

    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.

  • How I can create a file association?