You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET自定义安装获取用户输入,如何在主应用中读取该值?

问题描述

使用VB.NET开发时,已创建安装项目实现自定义安装,在用户界面添加了TextBox(A)对话框,安装时能成功获取用户输入并通过消息框验证,但安装后的应用无法读取该输入值,需要在Windows窗体应用中编写代码将值显示到TextBoxInput文本框中。

安装项目当前代码:

Imports System.ComponentModel
Imports System.Configuration
Imports System.Configuration.Install

Public Class Installer1
    Inherits Installer

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Dim userinput As String

    Public Overrides Sub Install(stateSaver As IDictionary)
         MyBase.Install(stateSaver)
         userinput = Context.Parameters("UserInput") 'CustomActionData = /UserInput=[EDITA1]
                                                     'this is set in custom action property

         stateSaver.Add("UserInput", userinput)
         My.Settings.Save()
    End Sub

    Private Sub Installer1_AfterInstall(sender As Object, e As InstallEventArgs) Handles Me.AfterInstall
    
        MessageBox.Show("This is working " + userinput)  'This is added to confirm that the user
                                                         'input in Textbox (A) has been converted
                                                         'to string userinput                        

    End Sub
原因分析

当前代码中My.Settings.Save()保存的是安装程序项目的设置,而非主Windows窗体应用的设置,两者属于不同程序域,因此主应用无法读取该值。需要将用户输入保存到主应用可访问的位置,比如注册表或主应用的配置文件。

解决方案

方法一:通过注册表存储与读取

步骤1:修改安装项目代码,将输入写入注册表

在安装项目的Installer1类中,添加注册表写入逻辑(需引入Microsoft.Win32命名空间):

Imports System.ComponentModel
Imports System.Configuration
Imports System.Configuration.Install
Imports Microsoft.Win32 ' 添加此引用

Public Class Installer1
    Inherits Installer

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Dim userinput As String

    Public Overrides Sub Install(stateSaver As IDictionary)
         MyBase.Install(stateSaver)
         userinput = Context.Parameters("UserInput")
         stateSaver.Add("UserInput", userinput)

         ' 将用户输入写入注册表(路径可自定义)
         Dim regKey As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\YourAppName")
         regKey.SetValue("InstallUserInput", userinput)
         regKey.Close()
    End Sub

    Private Sub Installer1_AfterInstall(sender As Object, e As InstallEventArgs) Handles Me.AfterInstall
        MessageBox.Show("This is working " + userinput)
    End Sub

步骤2:在主Windows窗体应用中读取注册表值

在窗体的Load事件中添加读取逻辑:

Imports Microsoft.Win32

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 打开注册表项
        Dim regKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\YourAppName")
        If regKey IsNot Nothing Then
            ' 读取值,默认空字符串
            Dim savedInput As String = regKey.GetValue("InstallUserInput", "").ToString()
            TextBoxInput.Text = savedInput
            regKey.Close()
        End If
    End Sub
End Class

方法二:通过主应用配置文件存储与读取

步骤1:修改安装项目代码,写入主应用配置文件

在安装项目的Installer1类中,添加修改主应用配置文件的逻辑(需引入System.IO命名空间):

Imports System.ComponentModel
Imports System.Configuration
Imports System.Configuration.Install
Imports System.IO ' 添加此引用

Public Class Installer1
    Inherits Installer

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Dim userinput As String

    Public Overrides Sub Install(stateSaver As IDictionary)
         MyBase.Install(stateSaver)
         userinput = Context.Parameters("UserInput")
         stateSaver.Add("UserInput", userinput)

         ' 获取主应用的安装目录与配置文件路径
         Dim targetDir As String = Context.Parameters("TARGETDIR")
         Dim appConfigPath As String = Path.Combine(targetDir, "YourMainApp.exe.config")
         If File.Exists(appConfigPath) Then
             ' 打开配置文件
             Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(appConfigPath)
             ' 添加或更新AppSettings项
             If config.AppSettings.Settings("InstallUserInput") Is Nothing Then
                 config.AppSettings.Settings.Add("InstallUserInput", userinput)
             Else
                 config.AppSettings.Settings("InstallUserInput").Value = userinput
             End If
             ' 保存配置
             config.Save(ConfigurationSaveMode.Modified)
             ConfigurationManager.RefreshSection("appSettings")
         End If
    End Sub

    Private Sub Installer1_AfterInstall(sender As Object, e As InstallEventArgs) Handles Me.AfterInstall
        MessageBox.Show("This is working " + userinput)
    End Sub

步骤2:在主Windows窗体应用中读取配置值

在窗体的Load事件中添加读取逻辑:

Imports System.Configuration

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 读取AppSettings中的值
        Dim savedInput As String = ConfigurationManager.AppSettings("InstallUserInput")
        If Not String.IsNullOrEmpty(savedInput) Then
            TextBoxInput.Text = savedInput
        End If
    End Sub
End Class

注意:替换代码中的YourAppName为你的应用名称,YourMainApp.exe为主应用的可执行文件名。

内容的提问来源于stack exchange,提问作者DrSinChan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 21:00:35