在修复安装的安装程序中清除app.Config文件条目

时间:2011-10-24 08:22:38

标签: winforms installation

我为我的项目创建了一个安装项目。该项目通过asmx服务连接到实时数据库服务器。客户端将在某些服务器上部署Web服务后,将确定该URL。所以在安装项目中,我在安装项目的用户界面编辑器部分添加了一个“TextBoxes”对话框,其中我只启用了一个TextBox来获取已部署服务的URL。在我的项目中,我添加了一个在安装过程中执行的文件,我已按如下方式定义:

[RunInstaller(true)]
public class InstallerHelper : System.Configuration.Install.Installer
{
    // Other Code also exists that is not needed to be shown here<br/>    
    //.....
    // The following method gets executed during setup installation
    public override void Install(IDictionary stateSaver)
    {
        try
        {
            base.Install(stateSaver);

            //Proceed only if the Context object has some parameters
            if (Context.Parameters.Count != 0 && !string.IsNullOrEmpty(Context.Parameters["WEBSITEURL"]))
            {
                //Get the installation Folder's Path
                string installationFolder = Context.Parameters["INSTALLFOLDER"];

               // Get the Site's URL entered by Client
                string websiteUrl = Context.Parameters["WEBSITEURL"];

               //Create different Key Value pairs based on entered URL
                string[][] keyValues = {
                                new string[] {"SiteUrl",websiteUrl},
                                new string[] {"WebServiceURL", websiteUrl + "Users.asmx" },
                                new string[] {"TicketsServiceURL", websiteUrl + "Tickets.asmx"},
                                new string[] {"CampaignsAndProjetcsServiceURL", websiteUrl + "CampaignsAndProjetcs.asmx"},
                                new string[] {"EntitiesURL", websiteUrl + "Entities.asmx"},
                                new string[] {"AccountsURL", websiteUrl + "Accounts.asmx"},
                                new string[] {"TransactionsURL", websiteUrl + "Transactions.asmx"},
                                new string[] {"RelatedReportsURL", websiteUrl + "RelatedReports.asmx"},
                                new string[] {"GiftAidsURL", websiteUrl + "GiftAids.asmx"}
                               };

                // Load the app.Config file and store these values in it.
                //********************************************

                string configFilePath = installationFolder + @"\MyProject.exe.config";
                XmlDocument configuration = new XmlDocument();

                // Load App.Config File
                configuration.Load(configFilePath);

                //Add the values in it
                Utility.UpdateValue(keyValues, configuration);

             //Save configuration File
                configuration.Save(configFilePath);
              //********************************************<br/>
            }
        }
        catch (Exception ex)
        {
            throw new InstallException("The following Error(s) occured during installation. \n " + ex.Message);
        }
    }

}

此处我将项目的App.Config中输入的URL和其他一些生成的不同Web服务的URL存储在Project中以访问数据。

当我安装安装程序的新副本时,它工作正常,但当我尝试通过再次执行Setup.exe文件来修复已安装的项目时出现问题。

修复过程不会要求我再次输入URL,并且在首次安装期间存储在App.Config中的项目也会丢失。所以整个应用程序停止工作。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

一种好的方法是在某处保存此自定义信息,并在维护期间使用搜索检索它:

这样修复将从注册表中读取属性值并恢复原始属性。

可以通过在解决方案资源管理器中选择安装项目并单击其顶部窗格上的相应按钮来打开注册表编辑器和启动条件编辑器。