PDA

View Full Version : ایجاد تنظیمات برای برنامه با ini



ftmotlagh
یک شنبه 14 بهمن 1386, 16:51 عصر
سلام
اگه می شه دستوراتی که برای ساخت تنظیمات برای برنامه با ini هست رو بگید...
تو تالار هم جستجو کردم ولی چیزی پیدا نکردم...
مرسی.

MohsenPS
یک شنبه 14 بهمن 1386, 17:47 عصر
برای ذخیره و بازیابی Config,روش فوق العاده ساده تری در Net. فراهم شده , کافیه خصوصیات مورد نظر رو در بخش Setting از صفحه Properties مربوط به پروژه مورد نظر تعریف کنید ، همه تنظیماتی که معرفی کردین از طریق My.Settings قابل تغییر و ذخیره و باز یابی اند .

shahrdar
یک شنبه 14 بهمن 1386, 18:36 عصر
'ذخیره اطلاعات بر روی فایل
Public Sub Write_INI(ByVal Section As String, ByVal Key As String, ByVal Value As String)
Dim x As New INIAccess()
x.INIWrite(Application.StartupPath & "\Setting.ini", Section, Key, Value)
End Sub
' لود اطلاعات از روی فایل
Public Function Read_INI(ByVal Section As String, ByVal Key As String, ByVal DefaltValue As String)
Dim x As New INIAccess()
Read_INI = x.INIRead(Application.StartupPath & "\Setting.ini", Section, Key, DefaltValue)
End Function

'کلاس
Public Class INIAccess
#Region "API Calls"
' standard API declarations for INI access
Private Declare Unicode Function WritePrivateProfileString _
Lib "kernel32" _
Alias "WritePrivateProfileStringW" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Int32
Private Declare Unicode Function GetPrivateProfileString _
Lib "kernel32" _
Alias "GetPrivateProfileStringW" _
(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
#End Region
#Region "--- INIRead ---"
Public Overloads Function INIRead(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String, ByVal DefaultValue As String) As String
' primary version of call gets single value given all parameters
Dim n As Int32
Dim sData As String

sData = Space$(1024) ' allocate some room
n = GetPrivateProfileString(SectionName, KeyName, DefaultValue, sData, sData.Length, INIPath)
If n > 0 Then ' return whatever it gave us
INIRead = sData.Substring(0, n)
Else
INIRead = ""
End If
End Function

Public Overloads Function INIRead(ByVal INIPath As String, _
ByVal SectionName As String, ByVal KeyName As String) As String
' overload 1 assumes zero-length default
Return INIRead(INIPath, SectionName, KeyName, "")
End Function

Public Overloads Function INIRead(ByVal INIPath As String, ByVal SectionName As String) As String
' overload 2 returns all keys in a given section of the given file
Return INIRead(INIPath, SectionName, Nothing, "")
End Function

Public Overloads Function INIRead(ByVal INIPath As String) As String
' overload 3 returns all section names given just path
Return INIRead(INIPath, Nothing, Nothing, "")
End Function
#End Region
#Region "--- INIWrite ---"
Public Sub INIWrite(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String, ByVal TheValue As String)
Call WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)
End Sub
#End Region
#Region "--- INIDelete ---"
Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String) ' delete single line from section
Call WritePrivateProfileString(SectionName, KeyName, Nothing, INIPath)
End Sub

Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String)
' delete section from INI file
Call WritePrivateProfileString(SectionName, Nothing, Nothing, INIPath)
End Sub
#End Region
End Class