首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在VB.net中更新.ini文件中的值?

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

Stack Overflow用户
提问于 2015-06-01 22:27:52
回答 2查看 4.2K关注 0票数 0

我有一个.ini文件,我应该修改一个字符串名称"Error“的值,它的默认值是"no":

这是.ini文件:

代码语言:javascript
代码运行次数:0
运行
复制
[Error]
value=no

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

EN

回答 2

Stack Overflow用户

发布于 2015-06-01 23:38:01

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

代码语言:javascript
代码运行次数:0
运行
复制
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

使用示例:

代码语言:javascript
代码运行次数:0
运行
复制
Dim objIniFile As New IniFile("c:\data.ini")
objIniFile.WriteString("Settings", "ClockTime", "12:59")
Dim strData As String = _
    objIniFile.GetString("Settings", "ClockTime", "(none)")
票数 1
EN

Stack Overflow用户

发布于 2015-06-02 00:28:03

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

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

下面是你使用它的方法:

代码语言:javascript
代码运行次数:0
运行
复制
Dim ini As New IniFile
ini.Load("path to your INI file")
ini.Sections("Error").Keys("value").Value = "yes"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30576143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档