使用 Visual Studio 安装项目重命名配置文件

发布于 2024-09-25 05:56:35 字数 449 浏览 7 评论 0原文

我的应用程序有一些配置文件,每个配置文件都特定于它所部署到的机器(开发、生产等)。我想将它们添加到安装项目中,使用单选按钮选择一个,然后创建一个脚本(或者可能是自定义操作?)将所选文件重命名为connectionStrings.config、settings.config等。

这对于安装项目是否可行/可能?

为了给您一个想法,我的配置可能如下所示:

DEV connectionStrings.config
PROD connectionStrings.config

用户在安装程序单选按钮 UI 中选择 DEV 或 PROD 后,我希望将所选配置重命名为

connectionStrings.config

考虑到它是 VS 安装项目,我有一种感觉要求太多了,我会得到一个有趣的答复,就像大多数设置项目问题一样:)

My applications have a handful of config files, each one particular to the machine it's being deployed to (dev, production, etc.) I'm wanting to add them to the setup project, use radio buttons to select one and then have a script (or maybe custom action?) rename the selected files to connectionStrings.config, settings.config, etc.

Is this feasible/possible with a setup project?

To give you an idea, my configs might look like this:

DEV connectionStrings.config
PROD connectionStrings.config

After the user chooses DEV or PROD in the installer radiobutton UI, I would like the chosen config to be renamed to

connectionStrings.config

Considering it's a VS setup project, I have a feeling I'm asking for way too much and that I will get an interesting response as most setup project questions do :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此岸叶落 2024-10-02 05:56:35

我创建了一个设置项目来设置连接字符串,并使用了以下内容,该项目非常适合我。

为安装创建 installer.cs 文件。
使用系统;

using System.Data.SqlClient;
using System.Xml;
using System.Configuration;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Configuration.Install;


namespace YOURNAMESPACE
{

    [RunInstaller(true)]
    public partial class installer : System.Configuration.Install.Installer
    {

        public installer()
        {
            InitializeComponent();
        }
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            try
            {

                string DatabaseString1 = "FULL NAME OF CONNECTION STRING";
                ConnectionConfigure(DatabaseString1);


            }
            catch (Exception e)
            {
                base.Rollback(savedState);
            }
        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }


        private void ConnectionConfigure(string DatabaseString)
        {
            string dataSource = "";
            dataSource = "Provider="+ Context.Parameters["InitialCatalog"]+ ";" + "Data Source=" + Context.Parameters["DataSource"];
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();

            string configFile = string.Concat(Assembly.GetExecutingAssembly().Location, ".config");
            map.ExeConfigFilename = configFile;
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.
            OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
            string connectionsection = config.ConnectionStrings.ConnectionStrings
            [DatabaseString].ConnectionString;


            ConnectionStringSettings connectionstring = null;
            if (connectionsection != null)
            {
                config.ConnectionStrings.ConnectionStrings.Remove(DatabaseString);

            }

            connectionstring = new ConnectionStringSettings(DatabaseString, dataSource);
            config.ConnectionStrings.ConnectionStrings.Add(connectionstring);

            config.Save(ConfigurationSaveMode.Modified, true);
            ConfigurationManager.RefreshSection("connectionStrings");

        }


    }
}

将项目输出添加到您的安装项目中,然后开始设置此安装项目。

右键单击安装项目

添加文本框并创建 UI

设置自定义操作

创建项目输出操作

自定义操作属性

这就是我设置我的方法(我附上了屏幕截图来帮助解释我的过程,但简而言之。创建安装项目和 installer.cs 文件。将项目输出添加到安装程序项目,添加 UI 以便用户可以输入连接字符串和/或连接字符串提供程序,添加自定义操作以便 installer.cs 文件可以读取输入,然后恭喜它应该更改连接字符串。
希望这有帮助。

I created a setup project to set connection strings and i used the following which works perfectly for me.

Create a installer.cs file for the setup.
using System;

using System.Data.SqlClient;
using System.Xml;
using System.Configuration;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Configuration.Install;


namespace YOURNAMESPACE
{

    [RunInstaller(true)]
    public partial class installer : System.Configuration.Install.Installer
    {

        public installer()
        {
            InitializeComponent();
        }
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            try
            {

                string DatabaseString1 = "FULL NAME OF CONNECTION STRING";
                ConnectionConfigure(DatabaseString1);


            }
            catch (Exception e)
            {
                base.Rollback(savedState);
            }
        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }


        private void ConnectionConfigure(string DatabaseString)
        {
            string dataSource = "";
            dataSource = "Provider="+ Context.Parameters["InitialCatalog"]+ ";" + "Data Source=" + Context.Parameters["DataSource"];
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();

            string configFile = string.Concat(Assembly.GetExecutingAssembly().Location, ".config");
            map.ExeConfigFilename = configFile;
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.
            OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
            string connectionsection = config.ConnectionStrings.ConnectionStrings
            [DatabaseString].ConnectionString;


            ConnectionStringSettings connectionstring = null;
            if (connectionsection != null)
            {
                config.ConnectionStrings.ConnectionStrings.Remove(DatabaseString);

            }

            connectionstring = new ConnectionStringSettings(DatabaseString, dataSource);
            config.ConnectionStrings.ConnectionStrings.Add(connectionstring);

            config.Save(ConfigurationSaveMode.Modified, true);
            ConfigurationManager.RefreshSection("connectionStrings");

        }


    }
}

Add project output to your setup project then begin to setup this setup project.

Right click setup project

Add text boxes and create the UI

Set custom actions

Create project output actions

Custom action properties

That is how i setup mine (I have attached screenshots to help explain my process but in short. Create setup project and installer.cs file. Add project output to setup project, add a UI so that the user can input a connection string and or provider for connection string, add custom actions so that the inputs can be read by the installer.cs file and then congratulations it should change the connection string.
Hope this helps.

剩一世无双 2024-10-02 05:56:35

我得出的结论是,这是不可能的,因为 VS 严重缺乏安装项目配置选项。相反,我在设置过程中添加了一个单选按钮控件,并为选项分配了一个变量名称。在文件系统中,我添加了所有配置文件,然后为每个配置文件设置条件。他们将值引用到我的单选按钮选择,以便在部署期间进行复制。

I've come to the conclusion that this is impossible due to VS severely lacking on setup project configuration options. Instead I added a radio button control during the setup process and assigned the choices a variable name. In the file system I added all of my config files and then set conditions to each one. They referenced values to my radio button choices in order to copy during deployment.

萌能量女王 2024-10-02 05:56:35

我已经这样做了很多次,基本上我只是安装两个具有不同名称的文件。应用程序可以询问用户要使用哪个文件,并可以随时更改它,因为许多用户对应用程序了解不够,无法在安装时做出此选择。

对于此类设置问题,您会得到有趣的答案,因为许多人希望在安装过程中配置应用程序。为什么不直接让安装程序安装文件并让应用程序进行自己的配置?

I've done this many times, and basically I just install both files with different names. The application can ask the user which file to use, and change it anytime they want because many users don't know enough about the application to make this choice at install time.

You get interesting answers to setup questions like this because many people want to configure the application during the installation. Why not just let the setup install the files and have the app do its own configuration?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文