使用Newtonsoft.Json进行的配置保存代码

VB.NET · 06-08 · 418 人浏览
Imports System.IO
Imports Newtonsoft.Json

Module ini读写模块
    Public Sub SaveConfig(config As Config, filePath As String)
        Dim json As String = JsonConvert.SerializeObject(config, Formatting.Indented)
        File.WriteAllText(filePath, json)
    End Sub

    Public Function LoadConfig(filePath As String) As Config
        If Not File.Exists(filePath) Then
            Return New Config()
        End If

        Dim json As String = File.ReadAllText(filePath)
        Return JsonConvert.DeserializeObject(Of Config)(json)
    End Function
End Module

Public Class Config
    Public Property Check As String
    Public Property username As String
    Public Property password As String

    Public Property model_str As String '模式
    Public Property file_shipin As String '视频文件
    Public Property file_yinpin As String '音频文件
    Public Property file_xunhuan As String '循环文件
    Public Property file_push As String '推流码
End Class

读取:

Dim configFilePath As String = "config.json"
Dim loadedConfig As Config = LoadConfig(configFilePath)
Dim 模式 As String = loadedConfig.model_str
Dim 视频 As String = loadedConfig.file_shipin

Dim 音频 As String = loadedConfig.file_yinpin
Dim 循环 As String = loadedConfig.file_xunhuan
Dim 推流码 As String = loadedConfig.file_push

写入:

Dim configFilePath As String = "Config.json"
Dim config As Config = LoadConfig(configFilePath)
config.model_str = ComboBox1.Text
config.file_shipin = TextBox1.Text
config.file_yinpin = TextBox2.Text
config.file_xunhuan = TextBox3.Text
config.file_push = TextBox4.Text
SaveConfig(config, configFilePath)
* 人工审核评论,通过后即可正常显示。软件建议请添加微信