想要将自定义安装时的输入值用于VB.net制作的应用程序中

问题描述 投票:0回答:1

我正在使用 VB.net 进行编程。我已经制作了安装项目来自定义安装我的主应用程序,并在用户界面中添加了文本框 (A) 对话框。安装时,我希望将文本框 (A) 对话框的输入保存为字符串,并希望应用程序在安装后每次运行时将这些值用作变量/常量 到目前为止,我已经在安装程序项目中尝试过这个:

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

因此,在安装结束时,会出现 msgbox,其中包含我在安装时在对话框中输入的文本框文本。所以我认为我的做法是对的。 现在,当我在安装后运行应用程序以在文本框中显示时,我无法检索此用户输入值。 要编写哪些代码来在 Windows 应用程序表单项目中使用此用户输入字符串值以将其显示在文本框(TextBoxInput)中

vb.net setup-project custom-action
1个回答
0
投票

我建议将您的设置保存在

%appdata%
文件夹中的 JSON 文件中。

创建一个包含您的设置的设置类,并使用共享的

Save
Load
方法:

Imports System.Text.Json
Public Class SettingsClass
  Public Property SettingValue1 As String
  Public Property SettingValue2 As Integer
  Public Property SettingArray3 As String()
  Public Shared Sub Save(settings As SettingsClass, filename As String)
    Dim s = JsonSerializer.Serialize(settings)
    System.IO.File.WriteAllText(filename, s)
  End Sub
  Public Shared Function Load(filename) As SettingsClass
    Dim s = IO.File.ReadAllText(filename)
    Return JsonSerializer.Deserialize(Of SettingsClass)(s)
  End Function
End Class

然后使用这些

Save
Load
方法来存储您的设置并将其加载回来:

Imports System

Module Program
  Sub Main(args As String())
    Dim appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 'different for each user on this PC
    'Dim programDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) 'same for all users on this PC
    Dim settingsFilename = IO.Path.Combine(appDataFolder, "Junk", "junkSettings.json")

    Dim mySettings = New SettingsClass With {.SettingValue1 = "Setting1", .SettingValue2 = 123, .SettingArray3 = {"a", "b", "c"}}

    SettingsClass.Save(mySettings, settingsFilename)

    Dim mySettings2 = SettingsClass.Load(settingsFilename)
    Console.WriteLine(mySettings2.SettingValue1)

    Console.WriteLine("Finished")
  End Sub
End Module

您可以在 Windows 文件资源管理器中键入

%appdata%
来查看该文件。

© www.soinside.com 2019 - 2024. All rights reserved.