简体   繁体   English

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

[英]How to update value in .ini file in VB.net?

I have .ini file that i should change the value of a string name "Error" its default value is "no": 我有一个.ini文件,我应该更改字符串名称“ Error”的值,其默认值为“ no”:

this is the .ini file : 这是.ini文件:

[Error]
value=no

How to update value in .ini file in VB.net ? 如何在VB.net中的.ini文件中更新值?

This post outlines a class you can use if you really want to use INI files; 如果您确实想使用INI文件,则本文概述了可以使用的类。 I've personally started using XML files for simple configs, though. 不过,我个人已经开始将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

Usage example: 用法示例:

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

Achrez you can use my library for manipulating with INI files, it's available on GitHub: 现在,您可以使用我的库来处理INI文件,该库在GitHub上可用:

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

Here is how you can use it: 使用方法如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM