How to manually register ClickOnce file associations after installation?

Microsoft's ClickOnce deployment system offers an easy-to-use file association manager which is built into the Visual Studio deployment process. Developers can add up to 8 file associations which will automatically be associated with their application when the user runs the ClickOnce installer.

I'd like to take it one step further, though: I want to allow users to manually add or remove additional file associations after installation from within my application.

I have two motivations for accomplishing this:

  • I won't "force" additional file associations on the user, which is how file associations through ClickOnce deployments are handled.
  • Users can add or remove their own unique file associations at their leisure.
  • The tricky part: Directly associating a filetype with an executable is not compatible with ClickOnce deployments

    Unlike traditional Windows applications, ClickOnce applications are not launched directly via their executable. Instead, they are launched via a special .appref-ms shortcut which handles the ClickOnce magic behind the scenes (automatic updates, locating the executable files in an obfuscated directory in %LOCALAPPDATA% , etc).

    If a ClickOnce-deployed app is opened directly via its executable, automatic updates are disabled and ClickOnce-specific methods will no longer function. Because of this, traditional registry file associations are not possible for my use case.

    How Visual Studio handles ClickOnce file associations

    The image below demonstrates Visual Studio 2010's built-in ClickOnce file association manager. This information is stored in the application's .manifest file and is added to the Windows registry upon installation.

    VS2010的ClickOnce文件关联管理器

    I've done some digging through my registry and have identified several entries made by the ClickOnce installer to associate filetypes with the ClickOnce deployed application.

    An example registry key for a ClickOnce filetype association I found in my registry:

    rundll32.exe dfshim.dll, ShOpenVerbExtension {ae74407a-1faa-4fda-9056-b178562cf98f} %1

    Where {ae74407a-1faa-4fda-9056-b178562cf98f} is a GUID used in several other locations in the registry for the associated application.

    My goal is to learn what information must be added to the registry (programmatically) to manually associate files with a ClickOnce deployed application.

    Any help is appreciated!


    You can figure out the registry keys to be added, using Windows Sysinternals Process Monitor (Previously known as RegMon).

    Capture Events when you install your app using ClickOnce with default file associations. It will record all the registry operations (lots of them).

    You would need to use some filters to easily identify the registry keys.


    Ok so after experiencing the same problem and doing some serious digging I think I have a solution. Evan Wondrasek was on the right path with the rundll command, and I was able to get the following working in a test environment.

    Essentially Uri Abramson was right that the Guid in the rundll.exe command is a reference to the clickonce application but it doesn't appear anywhere else in the registry. What it references is actually the other Keys in HKCUSoftwareClasses .

    To make the file reference work you first need a ProgID for your application. To my knowledge this can be whatever you want, or whatever you would normally set it to in the ClickOnce publishing menu in Visual Studio.

    You will also need to create a new Guid, and know the contents of your Application Reference (.appref-ms) file.

    For my example I will use the following:

    ProgId: FASERVER.TestApp
    Guid: ce6b2c69-ec54-4182-a87f-74c5dfe1a03e
    AppRefFileContents: http://SomeAddress.SomeDomain/TestApp/TestApp.application#TestApp.application, Culture=neutral, PublicKeyToken=df31b9b884b9be10, processorArchitecture=x86
    

    Please use your equivalent of the above values in the following code.

    To start, make a new registry key under HKCUSoftwareClasses called FASERVER.TestApp with the following strings:

    (Default) = SomeFileType
    AppId = TestApp.application, Culture=neutral, PublicKeyToken=df31b9b884b9be10, processorArchitecture=x86
    DeploymentProviderUrl = http://SomeAddress.SomeDomain/TestApp/TestApp.application
    Guid = {ce6b2c69-ec54-4182-a87f-74c5dfe1a03e}
    

    Once you have set these values create two new subKeys shell and shellex .

    Set the (Default) value of shell to open . Then create a subKey of shell called open , and a subKey of open called command . Set the (Default) value of command to the following:

    rundll32.exe dfshim.dll, ShOpenVerbExtension {ce6b2c69-ec54-4182-a87f-74c5dfe1a03e} %1
    

    Create a subKey of shellex called IconHandler and set its (Default) value to {ce6b2c69-ec54-4182-a87f-74c5dfe1a03e}

    Navigate to HKCUSoftwareClassesCLSID and create a new key {ce6b2c69-ec54-4182-a87f-74c5dfe1a03e} with the following strings:

    (Default) = Shell Icon Handler For Tif File
    AppId = TestApp.application, Culture=neutral, PublicKeyToken=df31b9b884b9be10, processorArchitecture=x86
    DeploymentProviderUrl = http://SomeAddress.SomeDomain/TestApp/TestApp.application
    IconFile = YourIconFile.ico
    

    Note: Your icon must be in your ClickOnce project.

    Now create a subKey under {ce6b2c69-ec54-4182-a87f-74c5dfe1a03e} called InProcServer32 with the following values:

    (Default) = dfshim.dll
    ThreadingModel = Apartment
    

    Lastly we need to link the previous keys with the file associations we desire. To do this navigate to HKCUSoftwareClasses and do the following for each desired file extension.

    Set the FileExt strings as follows:

    (Default) = FASERVER.TestApp
    AppId = TestApp.application, Culture=neutral, PublicKeyToken=df31b9b884b9be10, processorArchitecture=x86
    DeploymentProviderUrl = http://SomeAddress.SomeDomain/TestApp/TestApp.application
    Guid = {ce6b2c69-ec54-4182-a87f-74c5dfe1a03e}
    

    That's it! You should be done.

    Please note this will not set your application as the default program for that file type but will simply add a "ClickOnce Application Support Library" entry to the "Open With" menu.

    Hope this works for someone other than me and sorry for the long winded example.


    What about the Assoc command?

    http://support.microsoft.com/kb/184082


    assoc /?

    Displays or modifies file extension associations

    ASSOC [.ext[=[fileType]]]

    .ext Specifies the file extension to associate the file type with fileType Specifies the file type to associate with the file extension

    Type ASSOC without parameters to display the current file associations. If ASSOC is invoked with just a file extension, it displays the current file association for that file extension. Specify nothing for the file type and the command will delete the association for the file extension.


    File associations can be machine level or user level - Generally, stuff in the HKCR area is for machine-level file associations. Stuff in the HKCU/Software/Classes area is per-user stuff the user has customized (or you customized for them). Something like "assoc" will work on XP, 7, and probably Win 8 (althought i haven't used this cmd on win 8 yet).

    链接地址: http://www.djcxy.com/p/22726.html

    上一篇: Clickonce部署到多个环境

    下一篇: 如何在安装后手动注册ClickOnce文件关联?