All I want to do is open and read from a text file that contains a series of comma delimited text lines. The filename is fixed (“wisparam.txt”) and there is no user interaction to select it - it is hardwired into the code. I can’t figure out how to open the file correctly so it can be read from.
I tried:
dim infile as folderitem
infile=“wisparam.txt”
dim input as TextInputStream
input=TextInputStream.open(InFile)
But I get an error that says Type mismatch. Expected class FolderItem, but got TextLiteral infile=”wisparam.txt”
In my old QBASIC programming days, I’d open a file just by:
You are close. I remember QBASIC too. But Xojo starts to make sense after a while.
All of these do the same thing:
'option 1
var inFile as FolderItem
inFile = New FolderItem(“c:\abc\winsparam.txt”, FolderItem.PathModes.Native)
var ti as TextInputStream
ti= TextInputStream.Open(inFile)
'option 2
var inFile2 as New FolderItem(“c:\abc\winsparam.txt”, FolderItem.PathModes.Native)
var ti2 as TextInputStream = TextInputStream.Open(inFile2)
'option 3
var ti3 as TextInputStream = TextInputStream.Open(New FolderItem(“c:\abc\winsparam.txt”, FolderItem.PathModes.Native))
It’s just getting more confusing. My WisParam.TXT file is in the same folder as my sources code and when the application is installed, it will be placed in the same folder as the executable code, So I tried accessing the parameters file this way: Var wisparamFile As FolderItem =App.ExecutableFile.Parent.Child(“WisParam.TXT”)
When I test it, the software can’t find the file even though it is there. So I added MessageBox(wisparamFile.NativePath) to see the file’s path. It came back with.C:\Uasers\Me\AppData\Local\Temp\DebugConCoord\WisParam.TXT and of course that’s not where the file is. Why can’t I tell it to look explicitly in my source code folder instead of the Debug folder?
The thing that is really frustrating is if I install the parameters file in any folder besides the source code folder, I can easily access it. It’s only when I put it in the same folder with my code that Xojo goes debug-s**t crazy and refuses to access the file.
Never a good Idea to put files you will be working with in the executable folder, place them in something like SpecialFolder.ApplicationData.Child("YourAppName")
Dealing with FolderItems to refer to files instead of Strings takes some getting used to. I hated it at first too, but have come to realize how necessary it is when dealing with files across operating systems. There’s also a lot of convenience there.
So the issue of your file not being where you think it should be could be because every time you Debug your application, Xojo clears out the old debug folder and makes it again.
Make sure under your “Build Settings” for the OS you’re on, you create a “Copy Files” step. Drag your WisParam.TXT into it and make sure it’s set to go to the App Parent Folder.
This way, when you click Run, your app will be compiled, your text file will be copied to the app’s folder, and App.ExecutableFile.Parent.Child(“WisParam.TXT”) will actually exist.
If that file is read-only you could simply import it into your project so it becomes part of the compiled app. If you plan to also write to it it is best placed in your application’s application folder (see Brian’s suggestion). If there is some initial content of the file you could combine both approaches: import the initial file into your project, copy that file to the application folder in the Opening event of your app if it doesn’t yet exist there, and then write to (and read from) that copy.