Opc.Ua.Configuration.ConfigUtils.UninstallApplication C# (CSharp) Method

UninstallApplication() public static method

Uninstalls a UA application.
public static UninstallApplication ( InstalledApplication application ) : void
application InstalledApplication
return void
        public static void UninstallApplication(InstalledApplication application)
        {
            // validate the executable file.
            string executableFile = Utils.GetAbsoluteFilePath(application.ExecutableFile, true, true, false); 
            
            // get the default application name from the executable file.
            FileInfo executableFileInfo = new FileInfo(executableFile);
            string applicationName = executableFileInfo.Name.Substring(0, executableFileInfo.Name.Length-4);

            // choose a default configuration file.
            if (String.IsNullOrEmpty(application.ConfigurationFile))
            {
                application.ConfigurationFile = Utils.Format(
                    "{0}\\{1}.Config.xml", 
                    executableFileInfo.DirectoryName, 
                    applicationName);                
            }
            
            // install as a service.
            if (application.InstallAsService)
            {
                ServiceInstaller.UnInstallService(application.ApplicationName);
            }

            // validate the configuration file.
            string configurationFile = Utils.GetAbsoluteFilePath(application.ConfigurationFile, true, false, false); 
            
            if (configurationFile != null)
            {
                // load the current configuration.
                Opc.Ua.Security.SecuredApplication security = new Opc.Ua.Security.SecurityConfigurationManager().ReadConfiguration(configurationFile);

                // delete the application certificates.
                if (application.DeleteCertificatesOnUninstall)
                {
                    CertificateIdentifier id = Opc.Ua.Security.SecuredApplication.FromCertificateIdentifier(security.ApplicationCertificate);
                                        
                    // delete public key from trusted peers certificate store.
                    try
                    {
                        CertificateStoreIdentifier certificateStore = Opc.Ua.Security.SecuredApplication.FromCertificateStoreIdentifier(security.TrustedCertificateStore);

                        using (ICertificateStore store = certificateStore.OpenStore())
                        {
                            X509Certificate2 peerCertificate = store.FindByThumbprint(id.Thumbprint);

                            if (peerCertificate != null)
                            {
                                store.Delete(peerCertificate.Thumbprint);
                            }
                        }                      
                    }
                    catch (Exception e)
                    {
                        Utils.Trace("Could not delete certificate '{0}' from store. Error={1}", id, e.Message);
                    }    

                    // delete private key from application certificate store.
                    try
                    {
                        using (ICertificateStore store = id.OpenStore())
                        {
                            store.Delete(id.Thumbprint);
                        }
                    }
                    catch (Exception e)
                    {
                        Utils.Trace("Could not delete certificate '{0}' from store. Error={1}", id, e.Message);
                    }        
                    
                    // permentently delete any UA defined stores if they are now empty.
                    try
                    {
                        WindowsCertificateStore store = new WindowsCertificateStore();
                        store.Open("LocalMachine\\UA Applications");

                        if (store.Enumerate().Count == 0)
                        {
                            store.PermanentlyDeleteStore();
                        }
                    }
                    catch (Exception e)
                    {
                        Utils.Trace("Could not delete certificate '{0}' from store. Error={1}", id, e.Message);
                    }
                }

                // configure firewall.
                if (application.ConfigureFirewall)
                {
                    if (security.BaseAddresses != null && security.BaseAddresses.Count > 0)
                    {
                        try
                        {
                            RemoveFirewallAccess(security, executableFile);
                        }
                        catch (Exception e)
                        {
                            Utils.Trace("Could not remove firewall access for executable: {0}. Error={1}", executableFile, e.Message);
                        }
                    }
                }

                // remove the permissions for the HTTP endpoints used by the application.
                if (application.BaseAddresses != null && application.BaseAddresses.Count > 0)
                {
                    List<ApplicationAccessRule> noRules = new List<ApplicationAccessRule>();

                    for (int ii = 0; ii < application.BaseAddresses.Count; ii++)
                    {
                        Uri url = Utils.ParseUri(application.BaseAddresses[ii]);

                        if (url != null)
                        {
                            if (url.Scheme == Uri.UriSchemeHttp || url.Scheme == Uri.UriSchemeHttps)
                            {
                                try
                                {
                                    HttpAccessRule.SetAccessRules(url, noRules, true);                                    
                                    Utils.Trace("Removed HTTP access rules for URL: {0}", url);    
                                }
                                catch (Exception e)
                                {
                                    Utils.Trace("Could not remove HTTP access rules for URL: {0}. Error={1}", url, e.Message);
                                }
                            }
                        }
                    }
                }
            }
        }