【问题标题】:How to update value in .ini file in VB.net?如何在 VB.net 中更新 .ini 文件中的值?
【发布时间】:2015-08-15 01:38:57
【问题描述】:

我有 .ini 文件,我应该更改字符串名称“Error”的值,其默认值为“no”:

这是 .ini 文件:

[Error]
value=no

如何在 VB.net 中更新 .ini 文件中的值?

【问题讨论】:

  • 你读得怎么样? INI 文件有点陈旧,不清楚是手动解析还是使用 PInvoke
  • 为什么不为此使用 XML 文件?
  • 为什么我应该使用 XML 文件我认为我可以使用 .ini 文件?!
  • Dim ini_file As New IniFile(Path & "\userTest.ini") Dim iniString = ini_file.GetString("Error", "value", "(none)") If iniString.Equals("不”)然后....否则....

标签: vb.net ini


【解决方案1】:

This 帖子概述了一个类,如果你真的想使用 INI 文件,你可以使用;不过,我个人已经开始使用 XML 文件进行简单的配置。

Public Class IniFile
 ' API functions
 Private Declare Ansi Function GetPrivateProfileString _
   Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
   (ByVal lpApplicationName As String, _
   ByVal lpKeyName As String, ByVal lpDefault As String, _
   ByVal lpReturnedString As System.Text.StringBuilder, _
   ByVal nSize As Integer, ByVal lpFileName As String) _
   As Integer
 Private Declare Ansi Function WritePrivateProfileString _
   Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
   (ByVal lpApplicationName As String, _
   ByVal lpKeyName As String, ByVal lpString As String, _
   ByVal lpFileName As String) As Integer
 Private Declare Ansi Function GetPrivateProfileInt _
   Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
   (ByVal lpApplicationName As String, _
   ByVal lpKeyName As String, ByVal nDefault As Integer, _
   ByVal lpFileName As String) As Integer
 Private Declare Ansi Function FlushPrivateProfileString _
   Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
   (ByVal lpApplicationName As Integer, _
   ByVal lpKeyName As Integer, ByVal lpString As Integer, _
   ByVal lpFileName As String) As Integer
 Dim strFilename As String

 ' Constructor, accepting a filename
 Public Sub New(ByVal Filename As String)
   strFilename = Filename
 End Sub

 ' Read-only filename property
 ReadOnly Property FileName() As String
   Get
       Return strFilename
   End Get
 End Property

 Public Function GetString(ByVal Section As String, _
   ByVal Key As String, ByVal [Default] As String) As String
   ' Returns a string from your INI file
   Dim intCharCount As Integer
   Dim objResult As New System.Text.StringBuilder(256)
   intCharCount = GetPrivateProfileString(Section, Key, _
      [Default], objResult, objResult.Capacity, strFilename)
   If intCharCount > 0 Then GetString = _
      Left(objResult.ToString, intCharCount)
 End Function

 Public Function GetInteger(ByVal Section As String, _
   ByVal Key As String, ByVal [Default] As Integer) As Integer
   ' Returns an integer from your INI file
   Return GetPrivateProfileInt(Section, Key, _
      [Default], strFilename)
 End Function

 Public Function GetBoolean(ByVal Section As String, _
   ByVal Key As String, ByVal [Default] As Boolean) As Boolean
   ' Returns a boolean from your INI file
   Return (GetPrivateProfileInt(Section, Key, _
      CInt([Default]), strFilename) = 1)
 End Function

 Public Sub WriteString(ByVal Section As String, _
   ByVal Key As String, ByVal Value As String)
   ' Writes a string to your INI file
   WritePrivateProfileString(Section, Key, Value, strFilename)
   Flush()
 End Sub

 Public Sub WriteInteger(ByVal Section As String, _
   ByVal Key As String, ByVal Value As Integer)
   ' Writes an integer to your INI file
   WriteString(Section, Key, CStr(Value))
   Flush()
 End Sub

 Public Sub WriteBoolean(ByVal Section As String, _
   ByVal Key As String, ByVal Value As Boolean)
   ' Writes a boolean to your INI file
   WriteString(Section, Key, CStr(CInt(Value)))
   Flush()
 End Sub

 Private Sub Flush()
   ' Stores all the cached changes to your INI file
   FlushPrivateProfileString(0, 0, 0, strFilename)
 End Sub

End Class

使用示例:

Dim objIniFile As New IniFile("c:\data.ini")
objIniFile.WriteString("Settings", "ClockTime", "12:59")
Dim strData As String = _
    objIniFile.GetString("Settings", "ClockTime", "(none)")

【讨论】:

  • 本例“writeString”添加新字符串而不更新旧字符串!!?
【解决方案2】:

Ahrez,你可以使用我的库来处理 INI 文件,它可以在 GitHub 上找到:

https://github.com/MarioZ/MadMilkman.Ini/

这里是你如何使用它:

Dim ini As New IniFile
ini.Load("path to your INI file")
ini.Sections("Error").Keys("value").Value = "yes"

【讨论】:

  • 谢谢马里奥。我想知道您是否应该在帮助文件中创建一个名为“更新”的部分。这一行确实帮助所有代码变得更有意义。
  • 这仍然是最简单的选择。我现在已经在 2 个项目中使用了它,并且正在成为我的首选……谢谢马里奥。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 2015-10-02
  • 2013-06-01
  • 2010-09-28
  • 2020-06-22
  • 2011-08-04
  • 2015-09-01
相关资源
最近更新 更多