VB.NET 讀寫ini文件
第一次寫東西,不過還不是原創的,算是對自己學習路上的一點點記錄。希望看到的人能有一點的收獲。
大家相互鼓勵共同進步!
CSDN看到有人寫的方法,摘抄如下。
Public Class Form1
'************ini文件內容為*************************
'*[send] *
'*send1=1 *
'*send2=4 *
'*send3=3 *
'*IsSms=1 *
'**************************************************
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path As String
'ini文件名為 Send.ini
path = Application.StartupPath + "\Send.ini"
TextBox1.Text = GetINI("Send", "Send1", "", path)
TextBox2.Text = GetINI("Send", "Send2", "", path)
Dim IsSms As Integer = GetINI("Send", "IsSms", "", path)
If (IsSms = 1) Then
Me.RadioButton1.Checked = True
ElseIf (IsSms = 0) Then
Me.RadioButton2.Checked = True
End If
End Sub
'讀取ini文件內容
Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
'寫ini文件操作
Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
'讀ini API函數
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
'寫ini API函數
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim path As String
path = Application.StartupPath + "\Send.ini"
WriteINI("Send", "Send1", TextBox1.Text, path)
WriteINI("Send", "Send2", TextBox2.Text, path)
If (Me.RadioButton1.Checked = True) Then
WriteINI("Send", "IsSms", 1, path)
ElseIf (Me.RadioButton2.Checked = True) Then
WriteINI("Send", "IsSms", 0, path)
End If
MsgBox("配置設置已經成功!!!!")
Catch ex As Exception
MsgBox("錯誤!!!!")
End Try
End Sub
Code
原文地址 http://www.rzrgm.cn/aowind/archive/2005/02/06/102723.aspx
浙公網安備 33010602011771號