Search code examples
c#file-type

C# get file types before and after install of application


I'm currently trying to get the file type associations for ALL (I know big ask) File type associations on a machine, store this in a list (or dictionary if this is better?) then compare this against a list after an application has been installed (new list, new snapshot of associations) and outputs any differences in a new list (so a total of 3 lists, beforeinstall, afterinstall, comparelist)

For an example scenario: - Adobe Acrobat is associated with .pdf. So I have a snapshot of all associations with .pdf as acrobat.

I then install Adobe Reader - this takes the .pdf association and I run the list again and I have a list now with reader on the .pdf

I can then run a compare and output the differences and output this into a new format (listview/textfile)

I'd normally provide a code sample but in all honesty I haven't a clue where to start - I've looked around and can mainly find how to add an fta which is not what I want.

Cheers


Solution

  • Take a look at this Code Project article: System File Association

    The author gets list containing all extensions registered on the machine:

    var allExtensions = new List<string>();
    RegistryKey root = Registry.ClassesRoot;
    
    string[] subKeys = root.GetSubKeyNames();
    foreach (string subKey in subKeys) // now it's better to use Linq
    {
        if (subKey.StartsWith("."))
        {
            allExtensions.Add(subKey);
        }
    }
    

    And then gets associated progId:

    FileAssociationInfo fa = new FileAssociationInfo(extension);
    var progId = fa.ProgID; // .pdf progId is "SumatraPDF" (on my machine)
    

    Hope it helps.