Results 1 to 25 of 25

Thread: RESOLVED Read from *.ini file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Resolved RESOLVED Read from *.ini file

    Hey All,

    Well I can't figure this out...

    I have my program reading some info from an ini file on startup. If you
    manually start it...it reads the info just fine. But, if you have the program,
    auto start with windows, it will not read the ini file at all! Here's the code
    I'm using...

    VB Code:
    1. Private Const NUMOFTXTBOXES = 7
    2.  
    3. Public Function LoadINIFile()
    4.     Dim strArray(0 To NUMOFTXTBOXES - 1) As String
    5.     Dim i As Long
    6.     Dim FileHandle%
    7.     Dim strFileName As String
    8.     Dim SourceFile As String
    9.    
    10.     If Right$(App.path, 1) = "\" Then
    11.        SourceFile = App.path
    12.     Else
    13.        SourceFile = App.path & "\"
    14.     End If
    15.    
    16.     strFileName = SourceFile & "myfile.ini"
    17.  
    18.     FileHandle% = FreeFile
    19.     i = 0
    20.  
    21.     Open strFileName For Input As #FileHandle%
    22.     Do While Not EOF(FileHandle%)
    23.        Line Input #FileHandle%, strArray(i)
    24.        i = i + 1
    25.     Loop
    26.  
    27.     Label1.Caption = strArray(0)
    28.     Label2.Caption = strArray(1)
    29.     Label3.Caption = strArray(2)
    30.     Label4.Caption = strArray(3)
    31.     Label5.Caption = strArray(4)
    32.     Label6.Caption = strArray(5)
    33.     Label7.Caption = strArray(6)
    34.  
    35.     Close FileHandle%
    36.     DoEvents
    37. End Function

    Any Ideas?

    Thanks in advance,
    Ron
    Last edited by rdcody; Oct 12th, 2005 at 05:37 PM.

  2. #2
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Read from *.ini file

    When are you calling LoadINIFile? Put it in the form_onload function and it should run fine every time.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    form_onload?

    Well this may be a stupid question...where is that?

    I'm using VB5.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    I'm calling it from form_Load.

  5. #5
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Read from *.ini file

    Sorry, yea form_load. I mixed in another programming language there. These lines, I cant figure out why you are using them.
    VB Code:
    1. If Right$(App.path, 1) = "\" Then
    2.        SourceFile = App.path
    3.     Else
    4.        SourceFile = App.path & "\"
    5.     End If
    6.    
    7.     strFileName = SourceFile & "myfile.ini"
    you can do this with 1 line, and ignore that if/else statement.
    VB Code:
    1. strFileName = App.path & "/myfile.ini"

    Give this a try. I have not had a program have truoble reading from a file on startup. Is the INI file in the exact same spot as the programs EXE when it starts up as when you manually run it?
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Read from *.ini file

    For reading INI files, you have to use:
    VB Code:
    1. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    2. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    it's just adding the backslash in case app path doesn't return one

  8. #8
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Read from *.ini file

    CVMichael, I figured he would already have those lines, since it would not run manually started if he didnt.

    app.path doenst return the / ever. If you put the slash right before the file name, it will alway shave the correct path.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Read from *.ini file

    Quote Originally Posted by neicedover1982
    Sorry, yea form_load. I mixed in another programming language there. These lines, I cant figure out why you are using them.
    VB Code:
    1. If Right$(App.path, 1) = "\" Then
    2.        SourceFile = App.path
    3.     Else
    4.        SourceFile = App.path & "\"
    5.     End If
    6.    
    7.     strFileName = SourceFile & "myfile.ini"
    you can do this with 1 line, and ignore that if/else statement.
    VB Code:
    1. strFileName = App.path & "/myfile.ini"

    Give this a try. I have not had a program have truoble reading from a file on startup. Is the INI file in the exact same spot as the programs EXE when it starts up as when you manually run it?
    If you put the EXE in the "C:\" drive, then App.Path returns "C:\" (with the dir separator), but when you put the exe in a directory, then it returns the path without the dir separator, so checking the the "\" is actually a good thing....

  10. #10
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Read from *.ini file

    Ok, i jumped to a conclusion. I never, and have never seen a program, get put in the C:\ folder. They are always somewhere else. And i assumed he had a folder elsewhere since he had files along with the EXE, and leaving them loose in the C:\ is just a bad idea.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Read from *.ini file

    Quote Originally Posted by neicedover1982
    CVMichael, I figured he would already have those lines, since it would not run manually started if he didnt.
    It seems that he is using this code to read the INI:
    VB Code:
    1. Open strFileName For Input As #FileHandle%
    2.     Do While Not EOF(FileHandle%)
    3.        Line Input #FileHandle%, strArray(i)
    4.        i = i + 1
    5.     Loop
    Wich is not the recommended way to read an INI....

  12. #12
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Read from *.ini file

    Quote Originally Posted by neicedover1982
    Ok, i jumped to a conclusion. I never, and have never seen a program, get put in the C:\ folder. They are always somewhere else.
    Well, yea, I agree with that.... you rarely see it in the C:\...

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    yes...the ini file is in the same directory as the exe....what code I use to get
    the app path isn't the problem guys. It's how come I can manually start the
    program and it loads the file just fine, but if windows starts the program...
    it'won't!

  14. #14
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Read from *.ini file

    Its ok. I use INI files and read them like regular text files, since all the files I use are INI in other parts of my programs.

    rdcody, you might want to change the INI to TXT in the file and the program. Since your not actually using the INI the way an INI gets used. I guess I do it improper, no sense you should too.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  15. #15
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Read from *.ini file

    You really should post all the code, since your issue really isnt with what you actually posted.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    I did post the problem...didn't I?

  17. #17
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Read from *.ini file

    You only posted the function your calling. Where is the form_load code?
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    Hey CVMichael,

    How would I use the GetPrivateProfileString, WritePrivateProfileString with my
    code?

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    Well what in the world are you talking about?

    Private Sub Form_Load()
    Call LoadINIFile
    End Sub

  20. #20
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Read from *.ini file

    In your code put a mesage box displaying the file path, just before opening it.
    Recompile the exe, then start the exe both ways, and see what is the difference.
    Like this:
    VB Code:
    1. MsgBox "File Name: " & strFileName
    2.  
    3.     Open strFileName For Input As #FileHandle%
    4.     Do While Not EOF(FileHandle%)
    5.        Line Input #FileHandle%, strArray(i)
    6.        i = i + 1
    7.     Loop

  21. #21
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Read from *.ini file

    I use INI files for my help index for one of my games. I use this code to get a specific line from the file.
    VB Code:
    1. ' Write out the help topic contents
    2.     Dim topicNumber As String
    3.     topicNumber = lstTopics.ListIndex
    4.     Dim hFile As Long
    5.    Dim sFilename As String
    6.     fileName = ReadString(topicNumber, "File", App.Path & "/Files/Help.ini")
    7.  
    8.    sFilename = App.Path & "/Files/Help/" & fileName
    9.    
    10.   'obtain the next free file handle from the system
    11.   'and load the file into the textbox
    12.    hFile = FreeFile
    13.    Open sFilename For Input As #hFile
    14.       txtContents.Text = Input$(LOF(hFile), hFile)
    15.    Close #hFile
    The INI file is set up as...
    [0]
    Topic=Cheat Codes
    File=cheats.txt

    The code looks at the section "0" and gets the text after the "File=".
    You really done need to use this code since your just reading the entire text file. You would use it if you were had your INI file like,
    [File]
    1=text1
    2=text2
    3=text3
    you would write the code as..
    VB Code:
    1. Label1.Caption = ReadString("File", "1", App.Path & "/myfile.ini")
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  22. #22
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Read from *.ini file

    Quote Originally Posted by rdcody
    Hey CVMichael,

    How would I use the GetPrivateProfileString, WritePrivateProfileString with my
    code?
    Like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    4. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    5.  
    6. Public Function LoadINIFile()
    7.     Dim strFileName As String
    8.    
    9.     If Right$(App.Path, 1) = "\" Then
    10.        strFileName = App.Path
    11.     Else
    12.        strFileName = App.Path & "\"
    13.     End If
    14.    
    15.     strFileName = strFileName & "myfile.ini"
    16.    
    17.     Label1.Caption = ReadIni(App.EXEName, "TextBox1", strFileName)
    18.     Label2.Caption = ReadIni(App.EXEName, "TextBox2", strFileName)
    19.     Label3.Caption = ReadIni(App.EXEName, "TextBox3", strFileName)
    20.     Label4.Caption = ReadIni(App.EXEName, "TextBox4", strFileName)
    21.     Label5.Caption = ReadIni(App.EXEName, "TextBox5", strFileName)
    22.     Label6.Caption = ReadIni(App.EXEName, "TextBox6", strFileName)
    23.     Label7.Caption = ReadIni(App.EXEName, "TextBox7", strFileName)
    24. End Function
    25.  
    26. Public Function ReadIni(Section As String, Key As String, FileName As String) As String
    27.     Dim RetVal As String * 255, v As Long
    28.    
    29.     v = GetPrivateProfileString(Section, Key, "", RetVal, 255, FileName)
    30.    
    31.     ReadIni = Left(RetVal, v)
    32. End Function
    33.  
    34. Private Sub WriteINIFile()
    35.     Dim FileName As String, K As Long
    36.    
    37.     If Right$(App.Path, 1) = "\" Then
    38.        FileName = App.Path
    39.     Else
    40.        FileName = App.Path & "\"
    41.     End If
    42.    
    43.     FileName = FileName & "myfile.ini"
    44.    
    45.     WritePrivateProfileString App.EXEName, "TextBox1", Label1.Caption, FileName
    46.     WritePrivateProfileString App.EXEName, "TextBox2", Label2.Caption, FileName
    47.     WritePrivateProfileString App.EXEName, "TextBox3", Label3.Caption, FileName
    48.     WritePrivateProfileString App.EXEName, "TextBox4", Label4.Caption, FileName
    49.     WritePrivateProfileString App.EXEName, "TextBox5", Label5.Caption, FileName
    50.     WritePrivateProfileString App.EXEName, "TextBox6", Label6.Caption, FileName
    51.     WritePrivateProfileString App.EXEName, "TextBox7", Label7.Caption, FileName
    52. End Sub

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    Update:

    I changed the *.ini file to *.txt. I then hardcoded the path to the file.

    Works just like before... starts up manually just fine...but it will not load
    the .txt file with windows.

    I don't know.

  24. #24
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Read from *.ini file

    If windows starts the program from task scheduler for example, the program will run in a different user, so make sure that the file has permissions to everyone (just in case). And see then if it works or not ?

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Read from *.ini file

    Hey guys...

    in checking to see if the ini file exists before it calls the load routine,

    I had this line in there...

    VB Code:
    1. If Dir$(SourceFile & "myinifile.ini") = "" Then

    and I changed it to this...

    VB Code:
    1. MyFile = Dir$(SourceFile & "myinifile.ini")
    2. If MyFile = "" Then

    and it works now. Still doesn't explain why it would load manually, and not
    with windows...at least I don't know why.

    All I know is...if I hadn't quit drinking 7 or 8 years ago...I would have made a
    run to the liquor store! Whew!

    Thanks for all your input

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width