16,717
社区成员
发帖
与我相关
我的任务
分享
''此处为读取INI过程,以SQL SERVER 2000的连接信息为例
dim sconn as string
Dim s As StreamReader
s = New StreamReader(Application.StartupPath & "\info.ini")
Dim bdone As Boolean = False
Dim dline As String
Dim database, uid, pwd, server As String
While Not bdone
dline = s.ReadLine()
If dline = Nothing Then
bdone = True
Else
Select Case Mid(dline, 1, 6)
Case "databa"
database = Mid(dline, 10, dline.Length - 9)
Case "userna"
uid = Mid(dline, 10, dline.Length - 9)
Case "passwo"
pwd = Mid(dline, 10, dline.Length - 9)
Case "server"
server = Mid(dline, 12, dline.Length - 11)
End Select
End If
End While
s.Close()
Me.sConn = "database=" + database + ";uid=" + uid + ";pwd=" + pwd + ";server=" + server + ";"
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Integer, _
ByVal lpFileName As String) As Integer
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Integer
Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" ( _
ByVal lpAppName As String, _
ByVal lpReturnedString As StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" ( _
ByVal lpAppName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Integer
Public Const FILE_NAME = ".\test.ini"
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If (System.IO.File.Exists(FILE_NAME)) Then
Dim strCaption As New StringBuilder(256)
GetPrivateProfileString("Form", "Caption", "Default Caption", _
strCaption, strCaption.Capacity, FILE_NAME)
Me.Text = strCaption.ToString()
Me.Width = GetPrivateProfileInt("Form", "Width", Me.Width, FILE_NAME)
Me.Height = GetPrivateProfileInt("Form", "Height", Me.Height, FILE_NAME)
Me.Left = GetPrivateProfileInt("Form", "Left", Me.Left, FILE_NAME)
Me.Top = GetPrivateProfileInt("Form", "Top", Me.Top, FILE_NAME)
End If
End Sub
Protected Overrides Sub OnClosing( _
ByVal e As System.ComponentModel.CancelEventArgs)
Dim strCaption As String = Me.Text
WritePrivateProfileString("Form", "Caption", strCaption, FILE_NAME)
WritePrivateProfileString("Form", "Width", Me.Width.ToString(), FILE_NAME)
WritePrivateProfileString("Form", "Height", Me.Height.ToString(), FILE_NAME)
WritePrivateProfileString("Form", "Left", Me.Left.ToString(), FILE_NAME)
WritePrivateProfileString("Form", "Top", Me.Top.ToString(), FILE_NAME)
End Sub
End Class
Dim sr As IO.StreamReader = New IO.StreamReader("reportTool.ini")
Dim strSQL As String = ""
While sr.EndOfStream = False
strSQL += sr.ReadLine
End While
'聲明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
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
'定義讀取配置檔函數
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
'定義寫入配置檔函數
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
讀取
Try
Dim path As String
path = Application.StartupPath + "\reportTool.ini "
Me.txtTrioLabel.Text = GetINI("DB", "JDBC_USER", "", path)
Me.txtCFlag.Text = GetINI("DB", "JDBC_PASS", "", path)
....
Catch ex As Exception
' MsgBox(ex.Message)
End Try