c#-4.0 - 如何在setup工程中写入app.config并在程序中使用

标签 c#-4.0 installation app-config visual-studio-setup-proje

  • 我创建了一个继承自安装程序的 Installhelper.cs。
  • 使用这段代码 (A) 覆盖 Install() 方法。

  • 这些插入到 app.config 的值在 中不可用。 [项目名称].exe.config 文件 安装后创建的。
    我已经在 app.config 中手动添加了如下所示的部分(B)

    数据传递给安装程序类,但数据不会写入 app.config 字段。它们在安装过程中在创建的配置文件中保持不变。

    任何帮助是极大的赞赏。我在这上面花了将近一天的时间。

    代码 A:
    [RunInstaller(true)]
    public partial class Installation : System.Configuration.Install.Installer
    {
        public Installation()
        {
            InitializeComponent();
        }
    
        public override void Install(IDictionary stateSaver)
        {
            //base.Install(stateSaver);
    
            try
            {
                // In order to get the value from the textBox named 'EDITA1' I needed to add the line:
                // '/PathValue = [EDITA1]' to the CustomActionData property of the CustomAction We added. 
    
                string userName = Context.Parameters["userName"];
                string password = Context.Parameters["password"];
                string folderPath = Context.Parameters["path"];
    
                MessageBox.Show(userName);
                MessageBox.Show(password);
                MessageBox.Show(folderPath);
    
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
               //config.AppSettings.Settings.Add("userName", userName);
               //config.AppSettings.Settings.Add("password", password);
               //config.AppSettings.Settings.Add("foderPath", folderPath);
    
    
    
    
    
                config.AppSettings.Settings["userName"].Value = userName;
                config.AppSettings.Settings["password"].Value = password;
                config.AppSettings.Settings["foderPath"].Value = folderPath;
                config.Save(ConfigurationSaveMode.Full);
                ConfigurationManager.RefreshSection("appSettings");
    
    
    
    
            }
            catch (FormatException e)
            {
                string s = e.Message;
                throw e;
            }
        }
    }
    

    在应用程序配置中添加的部分是
    代码 B:
    <appSettings>
    <add key="userName" value="" />
    <add key="password" value="" />
    <add key="foderPath" value="" />
    </appSettings>
    

    最佳答案

    这种编码的问题是这一行

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    

    它没有给出安装过程中配置文件所依赖的路径。因此必须将其更改为
    string path = Path.Combine(new DirectoryInfo(Context.Parameters["assemblypath"].ToString()).Parent.FullName, "[project name].exe")
    
        Configuration config = ConfigurationManager.OpenExeConfiguration(path);
    

    现在它的工作。 :)

    关于c#-4.0 - 如何在setup工程中写入app.config并在程序中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11237702/

    相关文章:

    Android 应用程序,无需市场即可更新。未安装应用

    .net - 安全、可移植的加密配置值——可能吗?

    c# - 从自定义验证器中验证上下文的基本类型获取值

    c#-4.0 - AESManaged 加密/解密 - 填充无效且无法删除

    ionic-framework - ionic 包无法安装

    eclipse - 指定了未知版本的 Tomcat。汤姆猫 8.5 Linux

    多个 app.config 文件的 C# 问题

    c# - 为什么我有时有一个 app.config 而有时有一个 binaryname.dll.config 文件?

    c# - 在 selenium c# 的下拉列表中截取选项的屏幕截图

    c# - 无法将类型 'string' 隐式转换为 'char[]'