Read/Write a .TXT or .INI File


Alright first of all, create a .txt file called settings.txt, save it to a folder and put in this code:

VISUALBASIC Code
  1. [test1]
  2. test2=This is just a test.


Then, open VB6 and create a module. Change the name of that module to modINI. Then put this code into the module:

VISUALBASIC Code
  1. #If Win16 Then
  2. Declare Function WritePrivateProfileString Lib "Kernel" (ByVal AppName As String, ByVal keyname As String, ByVal NewString As String, ByVal FileName As String) As Integer
  3. Declare Function GetPrivateProfileString Lib "Kernel" Alias "GetPrivateProfilestring" (ByVal AppName As String, ByVal keyname As Any, ByVal default As String, ByVal ReturnedString As String, ByVal MAXSIZE As Integer, ByVal FileName As String) As Integer
  4. Declare Function ShellExecute Lib "shell.dll" (ByVal hwnd As Integer, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Integer) As Integer
  5. #Else
  6. 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
  7. 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
  8. Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  9. #End If
  10.  
  11. Function ReadINI(ByVal section, keyname, FileNaam, Optional standard As String) As String
  12. Dim str As String
  13. str = String(100000, Chr(0))
  14. ReadINI = Left(str, GetPrivateProfileString(section, ByVal keyname, standard, str, Len(str), FileNaam))
  15. End Function
  16.  
  17. Function WriteINI(ByVal sSection As String, ByVal sKeyName As String, ByVal sNewString As String, ByVal sFileNaam As String) As Integer
  18. WritePrivateProfileString sSection, sKeyName, sNewString, sFileNaam
  19. End Function


And that's the module that's gonna let us read/write stuff to the .ini file.

Then make a form called whatever you want. Make a text file called Text1 then double click on the background of your form. Then between

VISUALBASIC Code
  1. Private Sub Form_Load


And

VISUALBASIC Code
  1. End Sub


Put in:

VISUALBASIC Code
  1. Settings = Replace(App.Path & "\settings.txt", "\\", "\")


App.Path is the path of your application and "\settings.txt" is the path of your settings file. Put them together and it makes it so your settings file is in the same folder as your application. Then right after that put in:

VISUALBASIC Code
  1. Text1.Text = ReadINI("test1", "test2", Settings)


Now I'll define it. ReadINI is so that you can read the info from the settings file ("test1" is the name you put between [] in your settings file, "test2" is the word you put in after [] and before = in your settings file. Text1.Text is the info in your text box.

-------That will read the info from your settings file, now to write------------------------------------------

Create a command and name it Command1. Then double click it and put in this code:

VISUALBASIC Code
  1. WriteINI "test1", "test2", Text1.Text, Settings


Okay, now to define it. WriteINI is the command that's gonna write the info to your .txt file. "test1" and "test2" are the same thing as explained earlier and Text1.Text is the info thats gonna write itself to the settings file by clicking Command1.

Now that you've done all those steps click on "File" then "Make Project1.exe" and make it in the same folder as your settings file. Then start it up and you'll see that the text box info will be changed to "This is just a test.". If you would like to change it, you can, change the text box info to whatever you want and click Command1. Then close the project and re-open it and it will be changed! Now you know how to read/write info to a .txt/.ini file.

If you have any questions PM me or email me at: jalouna@hotmail.com

Enjoyyy!
vb_king's Avatar
Author:
Views:
553
Rating:
Posted on Saturday 26th July 2008 at 09:47 AM
Dalez
Dalez's Avatar
Okay, i have used this, and reading the file works perfect, but when trying to write it, it doesn't work.

In my txt file its this:

[Settings]
Name=Dalez;; (Y)
IP=127.0.0.1
Port=1234

My write code:
WriteINI "Settings", "Name", txtName.Text, Settings

txtName being the text box.

So why is this not working?

Thanks for the read, as it works perfect!
Posted on Friday 2nd November 2007 at 08:15 PM
vb_king
vb_king's Avatar
I dont use .NET I use Visual Basic 6
Posted on Tuesday 30th October 2007 at 10:33 PM
ShadowMage
ShadowMage's Avatar
Too bad this aint for VB.NET >.>;