|
-
Oct 12th, 2005, 11:37 AM
#1
Thread Starter
Addicted Member
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:
Private Const NUMOFTXTBOXES = 7
Public Function LoadINIFile()
Dim strArray(0 To NUMOFTXTBOXES - 1) As String
Dim i As Long
Dim FileHandle%
Dim strFileName As String
Dim SourceFile As String
If Right$(App.path, 1) = "\" Then
SourceFile = App.path
Else
SourceFile = App.path & "\"
End If
strFileName = SourceFile & "myfile.ini"
FileHandle% = FreeFile
i = 0
Open strFileName For Input As #FileHandle%
Do While Not EOF(FileHandle%)
Line Input #FileHandle%, strArray(i)
i = i + 1
Loop
Label1.Caption = strArray(0)
Label2.Caption = strArray(1)
Label3.Caption = strArray(2)
Label4.Caption = strArray(3)
Label5.Caption = strArray(4)
Label6.Caption = strArray(5)
Label7.Caption = strArray(6)
Close FileHandle%
DoEvents
End Function
Any Ideas?
Thanks in advance,
Ron
Last edited by rdcody; Oct 12th, 2005 at 05:37 PM.
-
Oct 12th, 2005, 11:56 AM
#2
Fanatic Member
Re: Read from *.ini file
When are you calling LoadINIFile? Put it in the form_onload function and it should run fine every time.
-
Oct 12th, 2005, 12:00 PM
#3
Thread Starter
Addicted Member
Re: Read from *.ini file
form_onload?
Well this may be a stupid question...where is that?
I'm using VB5.
-
Oct 12th, 2005, 12:01 PM
#4
Thread Starter
Addicted Member
Re: Read from *.ini file
I'm calling it from form_Load.
-
Oct 12th, 2005, 12:08 PM
#5
Fanatic Member
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:
If Right$(App.path, 1) = "\" Then
SourceFile = App.path
Else
SourceFile = App.path & "\"
End If
strFileName = SourceFile & "myfile.ini"
you can do this with 1 line, and ignore that if/else statement.
VB Code:
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?
-
Oct 12th, 2005, 12:10 PM
#6
Re: Read from *.ini file
For reading INI files, you have to use:
VB Code:
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
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
-
Oct 12th, 2005, 12:11 PM
#7
Thread Starter
Addicted Member
Re: Read from *.ini file
it's just adding the backslash in case app path doesn't return one
-
Oct 12th, 2005, 12:12 PM
#8
Fanatic Member
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.
-
Oct 12th, 2005, 12:13 PM
#9
Re: Read from *.ini file
 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:
If Right$(App.path, 1) = "\" Then
SourceFile = App.path
Else
SourceFile = App.path & "\"
End If
strFileName = SourceFile & "myfile.ini"
you can do this with 1 line, and ignore that if/else statement.
VB Code:
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....
-
Oct 12th, 2005, 12:14 PM
#10
Fanatic Member
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.
-
Oct 12th, 2005, 12:15 PM
#11
Re: Read from *.ini file
 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:
Open strFileName For Input As #FileHandle%
Do While Not EOF(FileHandle%)
Line Input #FileHandle%, strArray(i)
i = i + 1
Loop
Wich is not the recommended way to read an INI....
-
Oct 12th, 2005, 12:16 PM
#12
Re: Read from *.ini file
 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:\...
-
Oct 12th, 2005, 12:18 PM
#13
Thread Starter
Addicted Member
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!
-
Oct 12th, 2005, 12:18 PM
#14
Fanatic Member
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.
-
Oct 12th, 2005, 12:20 PM
#15
Fanatic Member
Re: Read from *.ini file
You really should post all the code, since your issue really isnt with what you actually posted.
-
Oct 12th, 2005, 12:22 PM
#16
Thread Starter
Addicted Member
Re: Read from *.ini file
I did post the problem...didn't I?
-
Oct 12th, 2005, 12:25 PM
#17
Fanatic Member
Re: Read from *.ini file
You only posted the function your calling. Where is the form_load code?
-
Oct 12th, 2005, 12:26 PM
#18
Thread Starter
Addicted Member
Re: Read from *.ini file
Hey CVMichael,
How would I use the GetPrivateProfileString, WritePrivateProfileString with my
code?
-
Oct 12th, 2005, 12:28 PM
#19
Thread Starter
Addicted Member
Re: Read from *.ini file
Well what in the world are you talking about?
Private Sub Form_Load()
Call LoadINIFile
End Sub
-
Oct 12th, 2005, 12:28 PM
#20
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:
MsgBox "File Name: " & strFileName
Open strFileName For Input As #FileHandle%
Do While Not EOF(FileHandle%)
Line Input #FileHandle%, strArray(i)
i = i + 1
Loop
-
Oct 12th, 2005, 12:31 PM
#21
Fanatic Member
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:
' Write out the help topic contents
Dim topicNumber As String
topicNumber = lstTopics.ListIndex
Dim hFile As Long
Dim sFilename As String
fileName = ReadString(topicNumber, "File", App.Path & "/Files/Help.ini")
sFilename = App.Path & "/Files/Help/" & fileName
'obtain the next free file handle from the system
'and load the file into the textbox
hFile = FreeFile
Open sFilename For Input As #hFile
txtContents.Text = Input$(LOF(hFile), hFile)
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:
Label1.Caption = ReadString("File", "1", App.Path & "/myfile.ini")
-
Oct 12th, 2005, 12:39 PM
#22
Re: Read from *.ini file
 Originally Posted by rdcody
Hey CVMichael,
How would I use the GetPrivateProfileString, WritePrivateProfileString with my
code?
Like this:
VB Code:
Option Explicit
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
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
Public Function LoadINIFile()
Dim strFileName As String
If Right$(App.Path, 1) = "\" Then
strFileName = App.Path
Else
strFileName = App.Path & "\"
End If
strFileName = strFileName & "myfile.ini"
Label1.Caption = ReadIni(App.EXEName, "TextBox1", strFileName)
Label2.Caption = ReadIni(App.EXEName, "TextBox2", strFileName)
Label3.Caption = ReadIni(App.EXEName, "TextBox3", strFileName)
Label4.Caption = ReadIni(App.EXEName, "TextBox4", strFileName)
Label5.Caption = ReadIni(App.EXEName, "TextBox5", strFileName)
Label6.Caption = ReadIni(App.EXEName, "TextBox6", strFileName)
Label7.Caption = ReadIni(App.EXEName, "TextBox7", strFileName)
End Function
Public Function ReadIni(Section As String, Key As String, FileName As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, FileName)
ReadIni = Left(RetVal, v)
End Function
Private Sub WriteINIFile()
Dim FileName As String, K As Long
If Right$(App.Path, 1) = "\" Then
FileName = App.Path
Else
FileName = App.Path & "\"
End If
FileName = FileName & "myfile.ini"
WritePrivateProfileString App.EXEName, "TextBox1", Label1.Caption, FileName
WritePrivateProfileString App.EXEName, "TextBox2", Label2.Caption, FileName
WritePrivateProfileString App.EXEName, "TextBox3", Label3.Caption, FileName
WritePrivateProfileString App.EXEName, "TextBox4", Label4.Caption, FileName
WritePrivateProfileString App.EXEName, "TextBox5", Label5.Caption, FileName
WritePrivateProfileString App.EXEName, "TextBox6", Label6.Caption, FileName
WritePrivateProfileString App.EXEName, "TextBox7", Label7.Caption, FileName
End Sub
-
Oct 12th, 2005, 01:10 PM
#23
Thread Starter
Addicted Member
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.
-
Oct 12th, 2005, 02:08 PM
#24
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 ?
-
Oct 12th, 2005, 05:36 PM
#25
Thread Starter
Addicted Member
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:
If Dir$(SourceFile & "myinifile.ini") = "" Then
and I changed it to this...
VB Code:
MyFile = Dir$(SourceFile & "myinifile.ini")
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|