1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
Imports System.Configuration Public Class MyExeConfig Private conf As Configuration Public Sub New() conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) End Sub Public Sub Save() conf.Save(ConfigurationSaveMode.Modified) ConfigurationManager.RefreshSection("appSettings") End Sub Public Function getValue(ByVal sKey As String) As String If checkKey(sKey) Then Return conf.AppSettings.Settings.Item(sKey).Value Else Return String.Empty End If End Function Public Sub Add(ByVal sKey As String, ByVal sValue As String) conf.AppSettings.Settings.Add(sKey, sValue) End Sub Public Sub Remove(ByVal sKey As String) conf.AppSettings.Settings.Remove(sKey) End Sub Public Sub Update(ByVal sKey As String, ByVal sValue As String) If checkKey(sKey) Then conf.AppSettings.Settings.Item(sKey).Value = sValue End If End Sub Public Function checkKey(ByVal sKey As String) As Boolean Dim bFind As Boolean = False Dim keyarr As String() = conf.AppSettings.Settings.AllKeys For Each k As String In keyarr If 0 = String.Compare(k, sKey, StringComparison.Ordinal) Then bFind = True Exit For End If Next Return bFind End Function End Class |
使用前先系窗体启动先NEW一下,MyExeConfig.exe.Config会自动加内容
1 2 3 |
<appSettings> <add key="iYouKey" value="4" /> </appSettings> |