RSS

ClickOnce Application,Expired Certificates & Public Key Token PART II

Thu, Jun 11, 2009

ClickOnce, visual studio 2008

In the first part of this series I wrote the possible solutions available for the ClicOnce certificate issue. if you have missed it please read it before continuing.

        This post is not going to be a long one, I  just want to point out a very important issue, when updating the ClickOnce using the method specified in PART I .
        When using Jim Harte’s Method to update a ClickOnce with a new certificate, it checks for the public token key of the current application to find its uninstall string, and then execute runDLL32 to trigger the uninstall. below is the code which gets the uninstall string:
          /// <summary>
          /// Gets the uninstall string for the current ClickOnce app from the Windows 
          /// Registry.
          /// </summary>
          /// <param name="PublicKeyToken">The public key token of the app.</param>
          /// <returns>The command line to execute that will uninstall the app.</returns>
           private static string GetUninstallString(string PublicKeyToken,
                                    out string DisplayName)
          {
           string uninstallString = null;
           string searchString = "PublicKeyToken=" + PublicKeyToken;
           RegistryKey uninstallKey = _
                Registry.CurrentUser.OpenSubKey
                          ("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
           string[] appKeyNames = uninstallKey.GetSubKeyNames();
           DisplayName = null;
           foreach(string appKeyName in appKeyNames)
          {
            RegistryKey appKey = uninstallKey.OpenSubKey(appKeyName);
            uninstallString = (string)appKey.GetValue("UninstallString");
            DisplayName = (string)appKey.GetValue("DisplayName");
            appKey.Close();
          
           if(uninstallString.Contains(searchString))
                              break;
           }
                      uninstallKey.Close();
                      return uninstallString;
                  }
           

           

          In the above code it goes through registry keys of all ClickOnce application to get the uninstall string. and if that uninstall string contains the Public Token key of the current application, it will uninstall the application with that particular uninstall string. this is completely ok if there is only one application with that public token key. the public key of an assembly name identifies the developer (or the organization). Most organizations use only one public key to sign their assemblies with. so what if there is more than 1 application which has the same public token key?

      • The code will uninstall the very first match in the above code and it might uninstall some other application, instead of uninstalling the one we need to uninstall.
      • and it will  also install the new application from new URL, but with a different name sometimes (Eg: application name -1).
      • And if it had uninstalled another application, whenever user launches this application it will prompt the uninstall screen, each time when there are more than 1 application with the same public token key.

    The Image below shows an uninstall string, which contains the application name, and public token key:

    debug

    To avoid this problem of wrong application getting uninstalled, I changed this part of the code from above code to :

    if(uninstallString.Contains(searchString)) break; 

    this:

    if(uninstallString.Contains(searchString)
                   && uninstallString.Contains("yourApp.application"))
                        break;

           

          In the code above, I’m Checking the ClickOnce application name with extension, along with the public token key to find out the correct uninstall string. that’s it!.

          Hope this solves your time in someway.

    Popularity: 9% [?]

    , , ,

    This post was written by:

    Aneef Fashir - who has written 32 posts on Aneef.Net.

    Software Engineer, Sri Lanka.

    Contact the author

    3 Comments For This Post

    1. CodeNiko Says:

      thanks for the updates Aneef. you saved my time, great work!, Microsoft should avoid these kind of lame issues, your posts has all the possible solutions and their cons.

      But there is a problem, Accessing registry might need the application to run on full trust right? correct me if i’m wrong.

      before leaving not forgetting to thank Jim and Danial for their hard work too.

      bye.

    2. Keefe Says:

      I am very new to ClickOnce.
      How do you use your code? I want to compile your code and create like reinstall.exe. When I run your code, how does it know what ClickOnce application un/install? Don’t you have to pass Application Name as parameter?

    3. Aneef Fashir Says:

      @Keefe,

      if you see the code above, I’m passing the application public key token, and the application name in to the method, so it will remove the exact application. We could do this only with public key token, but some providers use same certificate for all their applications, in that case it might uninstall wrong application. so I’m passing both.

      Hope it Helps

      Aneef

    2 Trackbacks For This Post

    1. ClickOnce Application,Expired Certificates & Public Key Token PART I | Aneef.Net Says:

      [...] just posted the Part II of this series is here. .Net Framework, Certificate, ClickOnce, Error, thawte, Update, verisign, Visual Studio, [...]

    2. ClickOnce Application,Expired Certificates & Public Key Token PART III – Pushing .net Framework 3.5 | Aneef.Net Says:

      [...] ClickOnce Application,Expired Certificates & Public Key Token PART II During my work on this ClickOnce update, we decided to upgrade the application to target .net framework 3.5 sp1. so I just set the target framework to 3.5 and set the required prerequisites (.net 3.5 SP1, and Windows Installer 3.1). and built the project and published it. and when I tried on a client machine which had .net framework 2.0, application got updated, prompted the uninstall message, and tried to launch the new ClickOnce application from the new URL, and BANG!. we get an error : The Error Message Says : Unable to install or run the application. The application [...]

    Leave a Reply

    Follow me