View previous topic :: View next topic |
Author |
Message |
Euzinho Dassilva Advanced Cheater
Reputation: 0
Joined: 08 Sep 2007 Posts: 62
|
Posted: Tue Oct 30, 2007 1:08 pm Post subject: Config.ini |
|
|
Hi,
Anyone knows how to create INI Files to config a system?
Like the (famous?) file config.ini, loaded at Application initialize with some instructions to config or determinate some actions to specific (or no) areas of the system, like positioning, window size, language file (if application supports) ...
Note: With Visual Basic
Note 2: I really want to learn and no only to know if anyone knows how to do. |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8580 Location: 127.0.0.1
|
Posted: Tue Oct 30, 2007 1:50 pm Post subject: |
|
|
Create a new module and add:
Code: | Option Explicit
'
' API Declarations
'
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 Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
'
' Global Defines
'
Global strINIFile As String
'
' ReadINI
' Reads A Value From An INI Section
'
Public Function ReadINI(Section, KeyName As String) As String
Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, GetPrivateProfileString(Section, ByVal KeyName, "", sRet, Len(sRet), strINIFile))
End Function
'
' WriteINI
' Writes A Value To An INI Section
'
Public Function WriteINI(sSection As String, sKeyName As String, sNewString As String) As Integer
Dim r
r = WritePrivateProfileString(sSection, sKeyName, sNewString, strINIFile)
End Function
|
Then inside your function, such as a command button, you can use something like this:
Code: | Private Sub Command1_Click()
'
' Set Path To INI File
'
strINIFile = App.Path & "\config.ini"
'
' Read Value From INI File
'
Dim strReadValue As String
strReadValue = ReadINI("section", "Key1")
'
' Write New Value To INI
'
WriteINI "section", "Key1", "NewValue"
End Sub |
Mind you this is a very basic way of doing this. _________________
- Retired. |
|
Back to top |
|
 |
Euzinho Dassilva Advanced Cheater
Reputation: 0
Joined: 08 Sep 2007 Posts: 62
|
Posted: Thu Nov 01, 2007 12:45 pm Post subject: |
|
|
Well, thank you for answer, but the code have some errors listed by VB Express, look
And, about the example usage, I can read the configs on load and write with a click button?
Or both "methods" must be in same event, like _click? |
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
|
Back to top |
|
 |
Euzinho Dassilva Advanced Cheater
Reputation: 0
Joined: 08 Sep 2007 Posts: 62
|
Posted: Fri Nov 02, 2007 7:21 am Post subject: |
|
|
These example can be added in a normal .VB file? Or must be in a Module? |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8580 Location: 127.0.0.1
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
|
Back to top |
|
 |
Euzinho Dassilva Advanced Cheater
Reputation: 0
Joined: 08 Sep 2007 Posts: 62
|
Posted: Sat Nov 03, 2007 4:54 am Post subject: |
|
|
Wiccaan, the second link that you post it's a fantastic class.
Thanks a lot, man. I'll +rep you for this help
The others answers are good too, but I believe be right +rep only one member, per topic, the member that really solved the question. |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8580 Location: 127.0.0.1
|
|
Back to top |
|
 |
Euzinho Dassilva Advanced Cheater
Reputation: 0
Joined: 08 Sep 2007 Posts: 62
|
Posted: Sat Nov 03, 2007 7:36 am Post subject: |
|
|
Well, I decided to use the second link. But there is some problems when I save the config file. Many times I must delete manually the file to generate a new and updated config.ini.
You know if exists another class with these resources (or more) but more efficient in this aspects? |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8580 Location: 127.0.0.1
|
Posted: Sat Nov 03, 2007 7:52 am Post subject: |
|
|
Euzinho Dassilva wrote: | Well, I decided to use the second link. But there is some problems when I save the config file. Many times I must delete manually the file to generate a new and updated config.ini.
You know if exists another class with these resources (or more) but more efficient in this aspects? |
The class should rewrite to the file if it exists. I'd say recheck the example code and make sure you are fully using the class correctly for opening and saving the file while editing it.
A few other examples:
http://www.mentalis.org/soft/class.qpx?id=6
http://www.developer.com/net/asp/article.php/3287991
If you still have issues, you could always use XML files instead as they are more of a standard now-adays when it comes to holding configuration information and such. _________________
- Retired. |
|
Back to top |
|
 |
|